Skip to content

Commit 4b568b4

Browse files
committed
Handle Case Where Both Format and Value Contain Quotation Mark
1 parent 3eee749 commit 4b568b4

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Formatter extends BaseFormatter
1414
* Matches any @ symbol that isn't enclosed in quotes.
1515
*/
1616
private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
17+
private const QUOTE_REPLACEMENT = "\u{fffe}"; // invalid Unicode character
1718

1819
/**
1920
* Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
@@ -130,9 +131,17 @@ public static function toFormattedString($value, string $format, ?array $callBac
130131
return str_replace('@', $value, $format);
131132
}
132133
//escape any dollar signs on the string, so they are not replaced with an empty value
133-
$value = str_replace('$', '\\$', (string) $value);
134-
135-
return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? $value);
134+
$value = str_replace(
135+
['$', '"'],
136+
['\\$', self::QUOTE_REPLACEMENT],
137+
(string) $value
138+
);
139+
140+
return str_replace(
141+
['"', self::QUOTE_REPLACEMENT],
142+
['', '"'],
143+
preg_replace(self::SYMBOL_AT, $value, $format) ?? $value
144+
);
136145
}
137146

138147
// If we have a text value, return it "as is"

tests/data/Style/NumberFormat.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,24 +1682,44 @@
16821682
'#,##0.00;;"---"',
16831683
],
16841684
'issue 4124' => ['1 HUF', 1, '#,##0_-[$HUF]'],
1685-
'issue 4242-0' => [
1685+
'issue 4242 General with dollar sign' => [
16861686
'General $200 - 200', // expected result
16871687
'General $200 - 200', // cell contents
16881688
NumberFormat::FORMAT_GENERAL, // cell style
16891689
],
1690-
'issue 4242-1' => [
1690+
'issue 4242 Text with dollar sign' => [
16911691
'Text $200 - 200',
16921692
'Text $200 - 200',
16931693
NumberFormat::FORMAT_TEXT,
16941694
],
1695-
'issue 4242-2' => [
1695+
'issue 4242 Text with quotes, format without' => [
16961696
'"Hello" she said and "Hello" I replied',
16971697
'"Hello" she said and "Hello" I replied',
16981698
NumberFormat::FORMAT_TEXT,
16991699
],
1700-
'issue 4242-3' => [
1700+
'issue 4242 Format with quotes, text without' => [
17011701
'Text: $200 - 200',
17021702
'$200 - 200',
17031703
'"Text: "' . NumberFormat::FORMAT_TEXT,
17041704
],
1705+
'issue 4242 single quote mark' => [
1706+
'"',
1707+
'"',
1708+
'@',
1709+
],
1710+
'issue 4242 dollar sign' => [
1711+
'$100',
1712+
'$100',
1713+
'@',
1714+
],
1715+
'issue 4242 repeat unquoted at signs' => [
1716+
'xy xy xy',
1717+
'xy',
1718+
'@ @ @',
1719+
],
1720+
'issue 4242 quotes in format and text' => [
1721+
'Text: "Hooray" for me',
1722+
'"Hooray" for me',
1723+
'"Text: "' . NumberFormat::FORMAT_TEXT,
1724+
],
17051725
];

0 commit comments

Comments
 (0)