trimming commas?

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
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

trimming commas?

Post by ssand »

I am using

Code: Select all

$value = trim($value, ",");
It doesn't seem to work. Do I need to use a different function for commas?

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that should be fine. Maybe your $value has extra "invisible" data in it?

Code: Select all

var_export(htmlentities($value,ENT_QUOTES));
may be of use..
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Why not str_replace() ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

because it was assumed he wants to only strip the commas from the beginning and end of the string
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Well it returns

Code: Select all

'69,192'
So I have

Code: Select all

var_export(htmlentities($value,ENT_QUOTES));
$value = trim($value, ",");
......
Then inserts to a database and it is setting $value as 69 ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

please post all relevant code
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

and you want it to return what from that example? 69192? Trim only removes the given characters from the edges of a string, not the inside. For that, str_replace() is often the best option.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Thanks, this seems to work.

Code: Select all

$value = str_replace(",", '', $value);
After Jcart's post I realized that was the problem. I didn't want from the beginning and end, I needed middle!

Edit
feyd wrote:and you want it to return what from that example? 69192? Trim only removes the given characters from the edges of a string, not the inside. For that, str_replace() is often the best option.
Yes, I should've posted that a little clearer. I wanted to keep all the numbers. Just remove the comma.
Last edited by ssand on Sun Feb 26, 2006 11:54 pm, edited 1 time in total.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Glad you got it working!

While we are talking about str_replace. How can I replace multiple things with a character?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nickman013 wrote:How can I replace multiple things with a character?
explain. :?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Umm the best way to explain it is like this...

for example.

I have this string with this in it "D,$%E->V"

I would like to know how to strip out the , $ % - > so it just displays DEV.

Thanks!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks that took out many lines out of my codes. I used str_replaces for each character I wanted out.

Thank You
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That'd be regex or multiple passes by str_replace. Using array's with str_replace work well in that instance.
Post Reply