Jump to content

[SOLVED] strip all characters except numeric


richrock

Recommended Posts

I've got a return on a DB search, and just need to return the numeric value, not the whole string.

 

Reason for this is a client's using a DB program which inserts extra garbage not needed - I'm returning the results on prices, eg : $5,000 USD

 

I just need it to return: 5000 -

 

so...... is there a way to either return just the numeric result or strip the other chars from the string?  I've no experience with regex (which I thought might do something) and checked out str_replace, but there's way too many variables.

 

TIA

 

Rich

Try this

 

$string = "$5,000";
$string =preg_replace("/[^0-9]/","", $string);

print $string;

 

Regards

Ranju

 

Thank you so much!  I inserted the returned value and it works!  I'm dealing with around 6 currencies, so this will be great for stripping and returning proper currency values, as some don't display....

 

Thanks again.

ranjuvs' method will work, but only if there are no decimals in the price. If there sometimes are, you can use this:

 

<?php
$str = '$5,000.50 USD';
list($str) = explode('.', $str);
$str = preg_replace('/[^0-9]/', '', $str);
echo $str;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.