Skip to content

Commit 690cb21

Browse files
committed
Handle Escaped Quote in Format
1 parent 4b568b4 commit 690cb21

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public static function toFormattedString($value, string $format, ?array $callBac
126126
}
127127
// For now we do not treat strings in sections, although section 4 of a format code affects strings
128128
// Process a single block format code containing @ for text substitution
129-
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
129+
$formatx = str_replace('\\"', self::QUOTE_REPLACEMENT, $format);
130+
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $formatx) === 1) {
130131
if (!str_contains($format, '"')) {
131132
return str_replace('@', $value, $format);
132133
}
@@ -140,7 +141,7 @@ public static function toFormattedString($value, string $format, ?array $callBac
140141
return str_replace(
141142
['"', self::QUOTE_REPLACEMENT],
142143
['', '"'],
143-
preg_replace(self::SYMBOL_AT, $value, $format) ?? $value
144+
preg_replace(self::SYMBOL_AT, $value, $formatx) ?? $value
144145
);
145146
}
146147

tests/data/Style/NumberFormat.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,4 +1722,9 @@
17221722
'"Hooray" for me',
17231723
'"Text: "' . NumberFormat::FORMAT_TEXT,
17241724
],
1725+
'issue 4242 escaped quote in format' => [
1726+
'"Hello"',
1727+
'Hello',
1728+
'\\"@\\"',
1729+
],
17251730
];

0 commit comments

Comments
 (0)