Page 1 of 1

trimming commas?

Posted: Sun Feb 26, 2006 11:26 pm
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.

Posted: Sun Feb 26, 2006 11:28 pm
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..

Posted: Sun Feb 26, 2006 11:34 pm
by nickman013
Why not str_replace() ?

Posted: Sun Feb 26, 2006 11:36 pm
by John Cartwright
because it was assumed he wants to only strip the commas from the beginning and end of the string

Posted: Sun Feb 26, 2006 11:37 pm
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 ?

Posted: Sun Feb 26, 2006 11:42 pm
by John Cartwright
please post all relevant code

Posted: Sun Feb 26, 2006 11:45 pm
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.

Posted: Sun Feb 26, 2006 11:51 pm
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.

Posted: Sun Feb 26, 2006 11:54 pm
by nickman013
Glad you got it working!

While we are talking about str_replace. How can I replace multiple things with a character?

Posted: Mon Feb 27, 2006 12:08 am
by feyd
nickman013 wrote:How can I replace multiple things with a character?
explain. :?

Posted: Mon Feb 27, 2006 12:13 am
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!

Posted: Mon Feb 27, 2006 12:21 am
by josh

Posted: Mon Feb 27, 2006 12:23 am
by nickman013
Thanks that took out many lines out of my codes. I used str_replaces for each character I wanted out.

Thank You

Posted: Mon Feb 27, 2006 12:23 am
by feyd
That'd be regex or multiple passes by str_replace. Using array's with str_replace work well in that instance.