[SOLVED] Trimming Variables

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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Trimming Variables

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

$trimmed = str_replace(' KS (66214)', '', $var);
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post 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)
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Problem Solved

Post 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(' / \(&#1111;0-9]+\)/','', $location);
	$location = trim($location);
Thanks for the help

Mike
Post Reply