Jump to content

Preg_Match expression


davefootball123

Recommended Posts

Hi everyone. Hopefully this is a fairly simple question. I have a text file seen here. http://www.sowx.ca/WUCN11CWTO.txt

I have been able to preg_match some other text successfully...for example...getting all the locations in that text file...however I'm not sure how to just get one location. How would I preg_match "SEVERE THUNDERSTORM WARNING FOR:" and "CITY OF HAMILTON" from that text file?

 

Any help would be awesome.

 

Thanks, Dave

Link to comment
https://forums.phpfreaks.com/topic/274867-preg_match-expression/
Share on other sites

This you don't need regular expressions for, as you have two static strings you want to match. Just use strpos () to find out whether or not they're in the file in question.

Thanks Christian, the problem is...sometimes there will locations where the SEVERE THUNDERSTORM WARNING ENDED FOR: in the text. and strpos from what I have seen just looks for the 2 matches in the whole text. What I'm looking for is it just under the SEVERE THUNDERSTORM WARNING FOR: part of the text.

 

Dave,

 

The expression I have right now is seen below. It matches severe thunderstorm warning for: and all the locations up to and including city of hamilton. What I need is just city of hamilton. None of the locations leading up to it.

/[\s\w]+SEVERE THUNDERSTORM WARNING FOR:+[\s\w\-]+CITY OF HAMILTON/

Hmm... I see. Then what you can do, is to limit the scope of what strpos () searches through.

 

Use it first to check if there is an "ENDED FOR" message, grab the starting position of it. Cut everything from that position and out away, assuming it comes after the "WARNING FOR" message. Then just search for the "WARNING FOR" message and city within what's left.

Simple and effective. ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.