From ed10ce03e29aede8eddc9a4541484d63eef076f6 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 14 Mar 2019 09:04:41 +0200 Subject: [PATCH] Twig nicenumber: do not use 0 + string casting hack --- system/src/Grav/Common/Twig/TwigExtension.php | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 9140c17da..ab15a8869 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -1229,31 +1229,39 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn /** * Returns a nicer more readable number * - * @param int|float $n - * @return bool|string + * @param int|float|string $n + * @return string|bool */ public function niceNumberFunc($n) { - // first strip any formatting; - $n = 0 + str_replace(',', '', $n); + if (!\is_float($n) && !\is_int($n)) { + if (!\is_string($n) || $n === '') { + return false; + } - // is this a number? - if (!is_numeric($n)) { - return false; + // Strip any thousand formatting and find the first number. + $list = array_filter(preg_split("/\D+/", str_replace(',', '', $n))); + $n = reset($list); + + if (!\is_numeric($n)) { + return false; + } + + $n = (float)$n; } // now filter it; if ($n > 1000000000000) { - return round(($n/1000000000000), 2).' t'; + return round($n/1000000000000, 2).' t'; } if ($n > 1000000000) { - return round(($n/1000000000), 2).' b'; + return round($n/1000000000, 2).' b'; } if ($n > 1000000) { - return round(($n/1000000), 2).' m'; + return round($n/1000000, 2).' m'; } if ($n > 1000) { - return round(($n/1000), 2).' k'; + return round($n/1000, 2).' k'; } return number_format($n);