invictive Posted November 27, 2009 Share Posted November 27, 2009 Hi guys, I'm trying to edit a php file that grabs the weather from an RSS feed and formats it for my phone. I want it to search for the following string: Sunday: XXXXX Low XX. High XX. and strip everything between the first : and the final . The XXX are variables. So it would simply return: Sunday I can get it to replace static text, but I have no idea how to get it to replace data with wildcards. So far all I have is: $string=preg_replace(array("/Sunday: /","/Sunday: /"), array("Sunday","Sunday"), $string); but all that does is replace Sunday: with Sunday, it still leaves all the crap after it. Any help would be greatly appreciated, Thanks, invictive Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 27, 2009 Share Posted November 27, 2009 That's only part of a larger string, right? Assuming the 'variables' are non-whitespace, try this: <?php echo preg_replace('~(Sunday): \S+ Low \S+ High \S+~i', '$1', $string); ?> If the string you provided is the full string, you could simply do <?php echo substr($string, 0, strpos($string, ':')); ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 $string = preg_replace('~([a-z]+):.+\.~i','$1',$string); Quote Link to comment Share on other sites More sharing options...
invictive Posted November 28, 2009 Author Share Posted November 28, 2009 Thanks guys, $string = preg_replace('~([a-z]+):.+\.~i','$1',$string); seemed to do the trick, however I noticed that sometimes the string is different. I will attach the files to properly demonstrate what I mean. The RSS file that the script is grabbing and parsing: http://www.weatheroffice.gc.ca/rss/city/bc-85_e.xml The output file as the script is currently configured: http://pastebin.ca/1690436 The script: http://pastebin.ca/1690431 As you can see sometimes the string is different and there is more data. Is there any way to have it keep everything before the : (the day) and remove everything after? (including the thanks a lot, -invictive Quote Link to comment Share on other sites More sharing options...
invictive Posted November 28, 2009 Author Share Posted November 28, 2009 Ok, I seem to have got it working ok, this is what I have: $string = preg_replace('~(Sunday+):[^<]+~i','$1',$string); $string = preg_replace('~(Sunday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Monday+):[^<]+~i','$1',$string); $string = preg_replace('~(Monday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Tuesday+):[^<]+~i','$1',$string); $string = preg_replace('~(Tuesday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Wednesday+):[^<]+~i','$1',$string); $string = preg_replace('~(Wednesday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Thursday+):[^<]+~i','$1',$string); $string = preg_replace('~(Thursday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Friday+):[^<]+~i','$1',$string); $string = preg_replace('~(Friday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Saturday+):[^<]+~i','$1',$string); $string = preg_replace('~(Saturday Night+):[^<]+~i','$1',$string); $string = preg_replace('~(Current Conditions+):[^<]+~i','$1',$string); does that look ok, is there anything I should do to clean it up a bit? Thanks! -invictive Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 28, 2009 Share Posted November 28, 2009 I would probably do it like this then: <?php $replace = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Current Conditions'); $string = preg_replace('~(<title>(?:' . implode('|', $replace) . ')[^:]*):[^<]+~i', '$1', $string); ?> Just make sure that the string in $replace doesn't contain any special regex chars or the delimiter in use. Quote Link to comment 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.