1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-13 18:14:26 +02:00

closes e107inc/e107#3113 parses a string into a db ready number format

value returned works also with floatval()
This commit is contained in:
Achim Ennenbach
2018-05-01 23:29:50 +02:00
parent 9726a70d2d
commit 2a1a6d4118
2 changed files with 64 additions and 21 deletions

View File

@@ -1792,20 +1792,37 @@ class e_model extends e_object
* @param string $value
* @return integer|float
*/
public function toNumber($value)
{
$larr = localeconv();
$search = array(
$larr['decimal_point'],
$larr['mon_decimal_point'],
$larr['thousands_sep'],
$larr['mon_thousands_sep'],
$larr['currency_symbol'],
$larr['int_curr_symbol']
);
$replace = array('.', '.', '', '', '', '');
// public function toNumber($value)
// {
// $larr = localeconv();
// $search = array(
// $larr['decimal_point'],
// $larr['mon_decimal_point'],
// $larr['thousands_sep'],
// $larr['mon_thousands_sep'],
// $larr['currency_symbol'],
// $larr['int_curr_symbol']
// );
// $replace = array('.', '.', '', '', '', '');
return str_replace($search, $replace, $value);
// return str_replace($search, $replace, $value);
// }
public 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)))
);
}
/**