Page 1 of 1
Trimming Variables
Posted: Mon Dec 06, 2004 2:26 am
by irishmike2004
Hi guys:
Here is a variable I am getting from parsing an XML document.
Code: Select all
$location = $valuesї$indexesї"dnam"]ї0]]ї"value"];
The line in the XML that is in this variable is:
Code: Select all
<dnam>Shawnee Mission, KS (66214)</dnam>
I need to trim the (66214) from the name (including the space between KS and (66214). I have no idea how to do that.
Help on this is appreciated.
Posted: Mon Dec 06, 2004 2:33 am
by rehfeld
Code: Select all
$trimmed = str_replace(' KS (66214)', '', $var);
Posted: Mon Dec 06, 2004 2:43 am
by mchaggis
Code: Select all
<?php
$trimmed = eregi_replace(' ([0-9+])$','', $var);
?>
The above will work with any number. I you have problems with the above, you could try escaping the braces, but I think it's ok like that (can never remember)
Posted: Mon Dec 06, 2004 11:52 am
by irishmike2004
not sure why the eregi_expression example did not work, but the str_replace did.
Thanks for your help and it is working.
Posted: Mon Dec 06, 2004 6:34 pm
by Benjamin
Dude that is only going to work on one number. Just want to make sure your aware of that. So if the zip code changes or you pull a record with a different city your script will break.
Problem Solved
Posted: Mon Dec 06, 2004 7:30 pm
by irishmike2004
Yes, I was aware it only works on one number... that is why I wrote the following code to fix it:
Code: Select all
$location = preg_replace(' / \(ї0-9]+\)/','', $location);
$location = trim($location);
Thanks for the help
Mike