Searching for specific characters within a string
Posted: Wed Feb 20, 2008 5:29 am
Hello all
Firstly I should say that I’m a newbie at PHP but learning quickly.
I am currently developing a weather informational website which is making use of a MySQL backend + dynamic updates from a weather station. As my weather station does not have a solar / UV sensor I am taking the missing data from METAR (Aviation met reports)
Basically I have loaded various PHP variables with data from METAR however the formatting of this data it is out of my control. So here is my question:
Purely for example: I have the following string:
var$ = "scattered cloud at 1200m - obscured fog - overcast at 300m"
Currently I am splitting the string using explode as in this code example:
$temp_clouds = explode("-", $metarcloudreport);
$clouds = $temp_cloudreport[0];
$var1=$temp_cloudreport[1];
$var2=$temp_cloudreport[2];
This code loads $clouds with the value “scattered” $var1 with “obscured fog” etc.
Unfortunately $var1 can at times be shorter or sometime be longer (data still separated by “-“. So if there are only two elements in this array my code generates an error with regards to third and if there are 4 elements then of course the 4th gets missed.
Question 1: Is there any way to ovoid this behaviour? Maybe continually looping until it hits the end of the “-“s
The code above only partly satisfies my requirements. What I would really like to do is search the entire string $var for a specific word such as "fog" and load that into a variable called $current_conditions. Is that possible and if so I would really appreciated a code example?
Anyway thank you for taking the time to read this and I look forward too and appreciate any help offered. .
Regards
Nikki
Firstly I should say that I’m a newbie at PHP but learning quickly.
I am currently developing a weather informational website which is making use of a MySQL backend + dynamic updates from a weather station. As my weather station does not have a solar / UV sensor I am taking the missing data from METAR (Aviation met reports)
Basically I have loaded various PHP variables with data from METAR however the formatting of this data it is out of my control. So here is my question:
Purely for example: I have the following string:
var$ = "scattered cloud at 1200m - obscured fog - overcast at 300m"
Currently I am splitting the string using explode as in this code example:
$temp_clouds = explode("-", $metarcloudreport);
$clouds = $temp_cloudreport[0];
$var1=$temp_cloudreport[1];
$var2=$temp_cloudreport[2];
This code loads $clouds with the value “scattered” $var1 with “obscured fog” etc.
Unfortunately $var1 can at times be shorter or sometime be longer (data still separated by “-“. So if there are only two elements in this array my code generates an error with regards to third and if there are 4 elements then of course the 4th gets missed.
Question 1: Is there any way to ovoid this behaviour? Maybe continually looping until it hits the end of the “-“s
The code above only partly satisfies my requirements. What I would really like to do is search the entire string $var for a specific word such as "fog" and load that into a variable called $current_conditions. Is that possible and if so I would really appreciated a code example?
Anyway thank you for taking the time to read this and I look forward too and appreciate any help offered. .
Regards
Nikki