richrock Posted April 3, 2009 Share Posted April 3, 2009 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 Link to comment https://forums.phpfreaks.com/topic/152372-solved-strip-all-characters-except-numeric/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 Is the first symbol always a $ and the last always USD? Link to comment https://forums.phpfreaks.com/topic/152372-solved-strip-all-characters-except-numeric/#findComment-800218 Share on other sites More sharing options...
ranjuvs Posted April 3, 2009 Share Posted April 3, 2009 Try this $string = "$5,000"; $string =preg_replace("/[^0-9]/","", $string); print $string; Regards Ranju Link to comment https://forums.phpfreaks.com/topic/152372-solved-strip-all-characters-except-numeric/#findComment-800238 Share on other sites More sharing options...
richrock Posted April 3, 2009 Author Share Posted April 3, 2009 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. Link to comment https://forums.phpfreaks.com/topic/152372-solved-strip-all-characters-except-numeric/#findComment-800248 Share on other sites More sharing options...
thebadbad Posted April 3, 2009 Share Posted April 3, 2009 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; ?> Link to comment https://forums.phpfreaks.com/topic/152372-solved-strip-all-characters-except-numeric/#findComment-800255 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.