Searching for specific characters within a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nikki_M
Forum Newbie
Posts: 2
Joined: Wed Feb 20, 2008 4:45 am

Searching for specific characters within a string

Post by Nikki_M »

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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Searching for specific characters within a string

Post by Stryks »

For the latter task of finding keywords, the following regex should help.

Code: Select all

<?php
$subject = "scattered cloud at 1200m - obscured fog - overcast at 300m";
preg_match_all('/((?:fog|overcast){1})/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
 
print_r($result); 
?>
I always seem to take the hard road with regex though, so it'd be good to get someone to verify that. Anyhow, you should be able to add extra keywords by adding |whatever after overcast.

Just worth noting that you could possibly run into trouble with partial words. Could use a revision if that is an issue.

Hope this helps

EDIT - The following should prevent partial words ...

Code: Select all

preg_match_all('/\b((?:fog|overcast))\b/i', $subject, $result, PREG_PATTERN_ORDER);
Nikki_M
Forum Newbie
Posts: 2
Joined: Wed Feb 20, 2008 4:45 am

Re: Searching for specific characters within a string

Post by Nikki_M »

Hi Stryks

Just tested your idea and its exactly what i'm looking for :D

Many thanks for taking the time to help me....


Regards
Nikki
Post Reply