preg_replace() help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

preg_replace() help

Post by php3ch0 »

Hi

I have got some text that has been scraped from an rss feed.

I want to get parts of the data but not others

I have been using preg replace to remove parts that I do not want but I also want to remove a whole chunk of text that is dynamic.

e.g

I want to change
Tonight:
Partly Cloudy.
Low: 4C. Chance of rain: 10%


Tomorrow:
Partly Cloudy.
High: 10C. Low: 5C. Chance of rain: 0%

Current conditions as of 6:50 PM:
Partly Cloudy. Temp: 11C. (feels like 11).
Humidity: 47%

Winds: 16 km/hr.
All times shown are local to Ashford, GBR.
I want to remove the Current conditions

how do I go about this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it has a pretty regular pattern...
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

if I try something like this

Code: Select all

preg_replace("Current.*GBR","",$text);
I get the following error message:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in c:\easyserv\www\al3\index.php on line 222
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

php3ch0 wrote:if I try something like this

Code: Select all

preg_replace("Current.*GBR","",$text);
I get the following error message:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in c:\easyserv\www\al3\index.php on line 222
The Preg functions are PCRE and thus need to have a start and end delimiter that match. Look at the first regex tutorial in the regex forum on these boards ;)

/Current.*?GBR/
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post by php3ch0 »

OK ive changed the code and the error has gone but it is not filtering what I want it to.

Code: Select all

echo preg_replace("/Current.*?GBR/","",$data);
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

with the current regex it will replace the exact thing you're trying to catch... use preg_match() instead
Post Reply