Jump to content

Preg_Match expression


Go to solution Solved by 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/

Edited by davefootball123

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. ;)

Edited by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.