function get_only_numbers($string){
$getonly = str_split("0123456789");
$string = str_split($string);
foreach($string as $i => $c){
if(!in_array($c, $getonly))
unset($string[$i]);
}
return implode("", $string);
}
echo get_only_numbers("U$ 499,50"); // prints 49950
This function is supposed to return only the numbers from a string. Has this function been coded properly?
I think a single call to preg_replace can do that as well:
preg_replace('/\D+/', '', 'U$ 499,50'); // returns "49950"
See is_numeric to further optimize your function so that you don't need the array comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With