From 43d90f073017de4c41b0f515b8219ed07f88732b Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 4 Nov 2016 13:26:20 +0100 Subject: [PATCH 1/2] Allow special characters in format, fixes #58 --- src/Currency.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Currency.php b/src/Currency.php index fffcd3b..3ed0d77 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -117,10 +117,14 @@ public function format($value, $code = null) $format = $this->getCurrencyProp($code, 'format'); // Value Regex - $valRegex = '/([0-9].*|)[0-9]/'; + $valRegex = '/(?:[0-9].*|)[0-9]/'; + + // Value string + preg_match($valRegex, $format, $match); + $valString = $match[0]; // Match decimal and thousand separators - preg_match_all('/[\s\',.!]/', $format, $separators); + preg_match_all('/[\s\',.!]/', $valString, $separators); if ($thousand = array_get($separators, '0.0', null)) { if ($thousand == '!') { @@ -142,7 +146,7 @@ public function format($value, $code = null) $value = number_format($value, $decimals, $decimal, $thousand); // Return the formatted measurement - return preg_replace($valRegex, $value, $format); + return str_replace($valString, $value, $format); } /** From 3f7ca8f1658ad47e2d58a9f4a27fe08b7eb10477 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 4 Nov 2016 14:48:29 +0100 Subject: [PATCH 2/2] Remove doubled matching of value format string --- src/Currency.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Currency.php b/src/Currency.php index 3ed0d77..4eafbf4 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -119,12 +119,13 @@ public function format($value, $code = null) // Value Regex $valRegex = '/(?:[0-9].*|)[0-9]/'; - // Value string - preg_match($valRegex, $format, $match); - $valString = $match[0]; + // Match value format string + preg_match($valRegex, $format, $valFormat); + + $valFormat = $valFormat[0]; // Match decimal and thousand separators - preg_match_all('/[\s\',.!]/', $valString, $separators); + preg_match_all('/[\s\',.!]/', $valFormat, $separators); if ($thousand = array_get($separators, '0.0', null)) { if ($thousand == '!') { @@ -134,11 +135,6 @@ public function format($value, $code = null) $decimal = array_get($separators, '0.1', null); - // Match format for decimals count - preg_match($valRegex, $format, $valFormat); - - $valFormat = array_get($valFormat, 0, 0); - // Count decimals length $decimals = $decimal ? strlen(substr(strrchr($valFormat, $decimal), 1)) : 0; @@ -146,7 +142,7 @@ public function format($value, $code = null) $value = number_format($value, $decimals, $decimal, $thousand); // Return the formatted measurement - return str_replace($valString, $value, $format); + return str_replace($valFormat, $value, $format); } /**