From 2a1a6d41181b308a037ccf8a8f915183c2563e5b Mon Sep 17 00:00:00 2001 From: Achim Ennenbach Date: Tue, 1 May 2018 23:29:50 +0200 Subject: [PATCH 1/2] closes e107inc/e107#3113 parses a string into a db ready number format value returned works also with floatval() --- e107_handlers/model_class.php | 43 ++++++++++++++++++++++++----------- e107_handlers/mysql_class.php | 42 +++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/e107_handlers/model_class.php b/e107_handlers/model_class.php index 8d716382f..4573a12f2 100755 --- a/e107_handlers/model_class.php +++ b/e107_handlers/model_class.php @@ -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))) + ); } /** diff --git a/e107_handlers/mysql_class.php b/e107_handlers/mysql_class.php index c2a7d91f0..352fb8244 100644 --- a/e107_handlers/mysql_class.php +++ b/e107_handlers/mysql_class.php @@ -1301,11 +1301,13 @@ class e_db_mysql case 'float': // fix - convert localized float numbers - $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('.', '.', '', '', '', ''); + // $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, floatval($fieldValue)); + // return str_replace($search, $replace, floatval($fieldValue)); + + return $this->_toNumber($fieldValue); break; case 'null': @@ -1360,11 +1362,12 @@ class e_db_mysql case 'float': // fix - convert localized float numbers - $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('.', '.', '', '', '', ''); + // $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, floatval($fieldValue)); + // return str_replace($search, $replace, floatval($fieldValue)); + return $this->_toNumber($fieldValue); break; case 'null': @@ -1436,6 +1439,29 @@ class e_db_mysql } + /** + * 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))) + ); + } /** From d01a97816f28a4a5a1c400feb715e890db27301b Mon Sep 17 00:00:00 2001 From: Achim Ennenbach Date: Fri, 4 May 2018 19:27:00 +0200 Subject: [PATCH 2/2] refactoring toNumber() into e_parse class --- e107_handlers/e_parse_class.php | 26 ++++++++++++++++++++++++ e107_handlers/model_class.php | 24 +++++----------------- e107_handlers/mysql_class.php | 29 ++------------------------- e107_handlers/validator_class.php | 33 ++++++++++++++++--------------- 4 files changed, 50 insertions(+), 62 deletions(-) diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 6beee9b6b..ad25cec31 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -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 diff --git a/e107_handlers/model_class.php b/e107_handlers/model_class.php index 4573a12f2..986a8f0a4 100755 --- a/e107_handlers/model_class.php +++ b/e107_handlers/model_class.php @@ -1792,6 +1792,7 @@ class e_model extends e_object * @param string $value * @return integer|float */ + // moved to e_parse // public function toNumber($value) // { // $larr = localeconv(); @@ -1807,23 +1808,6 @@ class e_model extends e_object // 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))) - ); - } /** * Convert object data to a string @@ -2721,7 +2705,8 @@ class e_front_model extends e_model { case 'int': case 'integer': - return intval($this->toNumber($value)); + //return intval($this->toNumber($value)); + return intval($tp->toNumber($value)); break; case 'safestr': @@ -2748,7 +2733,8 @@ class e_front_model extends e_model break; case 'float': - return $this->toNumber($value); + // return $this->toNumber($value); + return $tp->toNumber($value); break; case 'bool': diff --git a/e107_handlers/mysql_class.php b/e107_handlers/mysql_class.php index 352fb8244..13177476b 100644 --- a/e107_handlers/mysql_class.php +++ b/e107_handlers/mysql_class.php @@ -1307,7 +1307,7 @@ class e_db_mysql // return str_replace($search, $replace, floatval($fieldValue)); - return $this->_toNumber($fieldValue); + return e107::getParser()->toNumber($fieldValue); break; case 'null': @@ -1367,7 +1367,7 @@ class e_db_mysql // $replace = array('.', '.', '', '', '', ''); // return str_replace($search, $replace, floatval($fieldValue)); - return $this->_toNumber($fieldValue); + return e107::getParser()->toNumber($fieldValue); break; case 'null': @@ -1439,31 +1439,6 @@ class e_db_mysql } - /** - * 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))) - ); - } - - /** * @DEPRECATED Similar to db_Update(), but splits the variables and the 'WHERE' clause. diff --git a/e107_handlers/validator_class.php b/e107_handlers/validator_class.php index d000f31b5..b91fe03f2 100644 --- a/e107_handlers/validator_class.php +++ b/e107_handlers/validator_class.php @@ -577,7 +577,7 @@ class e_validator break; case 'float': - $value = $this->toNumber($value); + $value = e107::getParser()->toNumber($value); if(!is_numeric($value)) { $this->addValidateResult($name, self::ERR_FLOAT_EXPECTED); @@ -728,22 +728,23 @@ class e_validator break; } } - - 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('.', '.', '', '', '', ''); + + // moved to e_parse + // 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); + // } protected function parseMinMax($string) {