From 53ac682ba785824525118c70afb6889dae9782bf Mon Sep 17 00:00:00 2001 From: Aleksandr Borovikov Date: Thu, 30 May 2019 14:37:48 +0500 Subject: [PATCH] Fix #956: Incorrectly handled backslash-escaped space characters within number formats (#957) Backslash-escaped space characted sometimes not converted to quoted strings, so formatted string contains extra slashes. --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Style/NumberFormat.php | 2 +- tests/data/Style/NumberFormat.php | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84b638b9..81ad8bb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix handling for escaped enclosures and new lines in CSV Separator Inference - Fix MATCH an error was appearing when comparing strings against 0 (always true) +- Fix incorrectly handled backslash-escaped space characters in number format - Fix wrong calculation of highest column with specified row [#700](https://github.com/PHPOffice/PhpSpreadsheet/issues/700) - Fix VLOOKUP - Fix return type hint diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 3464acb6..37442249 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -614,7 +614,7 @@ class NumberFormat extends Supervisor } // Convert any other escaped characters to quoted strings, e.g. (\T to "T") - $format = preg_replace('/(\\\([^ ]))(?=(?:[^"]|"[^"]*")*$)/u', '"${2}"', $format); + $format = preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/u', '"${2}"', $format); // Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal) $sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format); diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index a70a1ddf..69590b94 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -88,6 +88,11 @@ return [ 12345.678900000001, '$ #,##0.000', ], + [ + '12,345.679 €', + 12345.678900000001, + '#,##0.000\ [$€-1]', + ], [ '5.68', 5.6788999999999996,