PHP 8.5.0 Alpha 1 available for testing

Voting

: min(one, five)?
(Example: nine)

The Note You're Voting On

brewal dot renault at gmail dot com
11 years ago
This function takes the last comma or dot (if any) to make a clean float, ignoring thousand separator, currency or any other letter :

function tofloat($num) {
$dotPos = strrpos($num, '.');
$commaPos = strrpos($num, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);

if (!$sep) {
return floatval(preg_replace("/[^0-9]/", "", $num));
}

return floatval(
preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
);
}

$num = '1.999,369€';
var_dump(tofloat($num)); // float(1999.369)
$otherNum = '126,564,789.33 m²';
var_dump(tofloat($otherNum)); // float(126564789.33)

Demo : http://codepad.org/NW4e9hQH

<< Back to user notes page

To Top