mirror of
https://github.com/e107inc/e107.git
synced 2025-08-14 02:24:08 +02:00
refactoring toNumber() into e_parse class
This commit is contained in:
@@ -2385,6 +2385,32 @@ class e_parse extends e_parser
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a string to a number (int/float)
|
||||
*
|
||||
* @param string $value
|
||||
* @return int|float
|
||||
*/
|
||||
function toNumber($value)
|
||||
{
|
||||
// adapted from: https://secure.php.net/manual/en/function.floatval.php#114486
|
||||
$dotPos = strrpos($value, '.');
|
||||
$commaPos = strrpos($value, ',');
|
||||
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
|
||||
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
|
||||
|
||||
if (!$sep) {
|
||||
return preg_replace("/[^-0-9]/", "", $value);
|
||||
}
|
||||
|
||||
return (
|
||||
preg_replace("/[^-0-9]/", "", substr($value, 0, $sep)) . '.' .
|
||||
preg_replace("/[^0-9]/", "", substr($value, $sep+1, strlen($value)))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Clean and Encode Ampersands '&' for output to browser.
|
||||
* @param string $text
|
||||
|
Reference in New Issue
Block a user