Page 1 of 1

How can I strip "$5,000" to just "5000"?

Posted: Mon Dec 29, 2008 12:59 pm
by carmen.smth1
Or any number in that format like $24,900 to just 24900. Then turn it back? I need to make a script that will take a price and automatically add a certain amount like $24,900 should be $26,900 but I need to strip the numbers before I can use the addition function.

Any ideas? Thank you!

Re: How can I strip "$5,000" to just "5000"?

Posted: Mon Dec 29, 2008 1:09 pm
by Mark Baker

Code: Select all

 
 
$string = '$24,900';
echo $string.'<br />';
 
$number = str_replace(array('$',','), array('',''), $string);
echo $number.'<br />';
 
$reformatted = '$'.number_format($number);
echo $reformatted.'<br />';
 
 

Re: How can I strip "$5,000" to just "5000"?

Posted: Mon Dec 29, 2008 2:02 pm
by Christopher
Or to get everything but digits:

Code: Select all

$number = preg_replace('/[^0-9]/', '', $string);

Re: How can I strip "$5,000" to just "5000"?

Posted: Mon Dec 29, 2008 2:34 pm
by jmut
Should be very careful with this. If this thingy is localazied you can get very funky results. As comman might mean fraction delimitar or thousands or god knows what else in different locales.
Your best option is to do the math before view and then properly render it. If you're doing a post processing and not in control of all...should be clear on what might expect.