Page 1 of 1

Regular Expression help

Posted: Mon Dec 06, 2004 4:12 pm
by irishmike2004
I am working with the preg_replace function and I can't seem to get the variable trimmed quite right. We solved the problem as long as the PHP script is run for my town always, but we want to use it in other locations as well.

The data inside the variable (I call $location) is as follows:

Shawnee, KS (66214)

We want to loose the space and (66214)... the code I tried is this:

Code: Select all

$location = preg_replace(' (ї0-9+])','', $location);
	$location = trim($location);
This makes the following output:

Shawnee, KS ()

I am not sure being as I am not good at defining regular expressions, can someone tell me what is wrong with my replace command.

earlier someone suggested that I use str_replace but that will break whenever we change the zipcode of the weather XML feed.

Thanks,

Posted: Mon Dec 06, 2004 4:33 pm
by timvw
try to escape ( and )

Tried Escapes and nothing

Posted: Mon Dec 06, 2004 4:45 pm
by irishmike2004
The first sequence I tried was this:

Code: Select all

$location = preg_replace(' \(ї0-9+]\)','', $location);
This takes the whole string away

the next one was this

Code: Select all

$location = preg_replace(' (\ї0-9+\])','', $location);
No Dice

Then I tried doubling the () with escapes like:

Code: Select all

$location = preg_replace(' \((ї0-9+])\)','', $location);
all this before I posted :-) Nothing I can think of seems to be working not sure if I am even (again) forming the pattern correctly.

Posted: Mon Dec 06, 2004 5:17 pm
by DrHoliday
The + sign has to go outside the [] (because you want to repeat the whole thing). Also i think you forgot the limiters (although i don't know if they are really needed). Try this:

Code: Select all

$location = preg_replace('/ \(ї0-9]+\)/', '', $location);
Wolfgang

Posted: Mon Dec 06, 2004 5:33 pm
by irishmike2004
That worked, problem solved. Thanks to all.