davefootball123 Posted February 23, 2013 Share Posted February 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274867-preg_match-expression/ Share on other sites More sharing options...
Christian F. Posted February 23, 2013 Share Posted February 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274867-preg_match-expression/#findComment-1414484 Share on other sites More sharing options...
davefootball123 Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) 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 February 23, 2013 by davefootball123 Quote Link to comment https://forums.phpfreaks.com/topic/274867-preg_match-expression/#findComment-1414485 Share on other sites More sharing options...
Christian F. Posted February 23, 2013 Share Posted February 23, 2013 (edited) 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 February 23, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/274867-preg_match-expression/#findComment-1414493 Share on other sites More sharing options...
Solution davefootball123 Posted February 23, 2013 Author Solution Share Posted February 23, 2013 Thanks again Christian. Managed to get what I needed with /[\s\w\-]?SEVERE THUNDERSTORM WARNING FOR:[\s\w\-]+(CITY OF HAMILTON)/ Quote Link to comment https://forums.phpfreaks.com/topic/274867-preg_match-expression/#findComment-1414507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.