Jump to content

preg_replace help


invictive

Recommended Posts

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

 

 

Link to comment
Share on other sites

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, ':'));
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.