From 01501b6ff2342430e5ee2c44188707c6a2cf6b63 Mon Sep 17 00:00:00 2001 From: Derek Bonner Date: Fri, 28 Sep 2018 05:28:40 -0700 Subject: [PATCH] Remove locale from format string to prevent formatting error (#644) When a formatting string has a locale in it an error can occur when outputting. For example when the format string with a locale such as `[$-1010409]#,##0.00;-#,##0.00` appears, a value of 9.98 comes back as $9.98. This is because at https://github.com/PHPOffice/PhpSpreadsheet/blob/1.4.0/src/PhpSpreadsheet/Style/NumberFormat.php#L711 the numberFormat regex will match to the zeros inside the locale ([$-1010409]). Attempts to adjust the numberFormat regex caused regressions in other tests. Adding another step to filter out the locale caused no regression. --- CHANGELOG.md | 3 +++ src/PhpSpreadsheet/Style/NumberFormat.php | 3 +++ tests/data/Style/NumberFormat.php | 20 ++++++++++++++++++++ tests/data/Style/NumberFormatDates.php | 10 ++++++++++ 4 files changed, 36 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85f3e7d5..a3d0130c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- Remove locale from formatting string - [#644](https://github.com/PHPOffice/PhpSpreadsheet/pull/644) + + ### Fixed - Allow iterators to go out of bounds with prev - [#587](https://github.com/PHPOffice/PhpSpreadsheet/issues/587) diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 63ef20ae..dc52a841 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -691,6 +691,9 @@ class NumberFormat extends Supervisor // Strip # $format = preg_replace('/\\#/', '0', $format); + // Remove locale code [$-###] + $format = preg_replace('/\[\$\-.*\]/', '', $format); + $n = '/\\[[^\\]]+\\]/'; $m = preg_replace($n, '', $format); $number_regex = '/(0+)(\\.?)(0*)/'; diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 196fa2c6..a7584ee4 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -186,4 +186,24 @@ return [ -1234567.8899999999, '0000:00.00', ], + [ + '18.952', + 18.952, + '[$-409]General', + ], + [ + '9.98', + 9.98, + '[$-409]#,##0.00;-#,##0.00', + ], + [ + '18.952', + 18.952, + '[$-1010409]General', + ], + [ + '9.98', + 9.98, + '[$-1010409]#,##0.00;-#,##0.00', + ], ]; diff --git a/tests/data/Style/NumberFormatDates.php b/tests/data/Style/NumberFormatDates.php index 5b74fbed..be8dc966 100644 --- a/tests/data/Style/NumberFormatDates.php +++ b/tests/data/Style/NumberFormatDates.php @@ -62,4 +62,14 @@ return [ 43270.603472222, 'hh:mm:ss\ AM/PM', ], + [ + '8/20/2018', + 43332, + '[$-409]m/d/yyyy', + ], + [ + '8/20/2018', + 43332, + '[$-1010409]m/d/yyyy', + ], ];