mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-31 04:28:51 +00:00
Handle Case Where Both Format and Value Contain Quotation Mark
This commit is contained in:
@@ -14,6 +14,7 @@ class Formatter extends BaseFormatter
|
||||
* Matches any @ symbol that isn't enclosed in quotes.
|
||||
*/
|
||||
private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
|
||||
private const QUOTE_REPLACEMENT = "\u{fffe}"; // invalid Unicode character
|
||||
|
||||
/**
|
||||
* Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
|
||||
@@ -130,9 +131,17 @@ class Formatter extends BaseFormatter
|
||||
return str_replace('@', $value, $format);
|
||||
}
|
||||
//escape any dollar signs on the string, so they are not replaced with an empty value
|
||||
$value = str_replace('$', '\\$', (string) $value);
|
||||
$value = str_replace(
|
||||
['$', '"'],
|
||||
['\\$', self::QUOTE_REPLACEMENT],
|
||||
(string) $value
|
||||
);
|
||||
|
||||
return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? $value);
|
||||
return str_replace(
|
||||
['"', self::QUOTE_REPLACEMENT],
|
||||
['', '"'],
|
||||
preg_replace(self::SYMBOL_AT, $value, $format) ?? $value
|
||||
);
|
||||
}
|
||||
|
||||
// If we have a text value, return it "as is"
|
||||
|
||||
@@ -1682,24 +1682,44 @@ return [
|
||||
'#,##0.00;;"---"',
|
||||
],
|
||||
'issue 4124' => ['1 HUF', 1, '#,##0_-[$HUF]'],
|
||||
'issue 4242-0' => [
|
||||
'issue 4242 General with dollar sign' => [
|
||||
'General $200 - 200', // expected result
|
||||
'General $200 - 200', // cell contents
|
||||
NumberFormat::FORMAT_GENERAL, // cell style
|
||||
],
|
||||
'issue 4242-1' => [
|
||||
'issue 4242 Text with dollar sign' => [
|
||||
'Text $200 - 200',
|
||||
'Text $200 - 200',
|
||||
NumberFormat::FORMAT_TEXT,
|
||||
],
|
||||
'issue 4242-2' => [
|
||||
'issue 4242 Text with quotes, format without' => [
|
||||
'"Hello" she said and "Hello" I replied',
|
||||
'"Hello" she said and "Hello" I replied',
|
||||
NumberFormat::FORMAT_TEXT,
|
||||
],
|
||||
'issue 4242-3' => [
|
||||
'issue 4242 Format with quotes, text without' => [
|
||||
'Text: $200 - 200',
|
||||
'$200 - 200',
|
||||
'"Text: "' . NumberFormat::FORMAT_TEXT,
|
||||
],
|
||||
'issue 4242 single quote mark' => [
|
||||
'"',
|
||||
'"',
|
||||
'@',
|
||||
],
|
||||
'issue 4242 dollar sign' => [
|
||||
'$100',
|
||||
'$100',
|
||||
'@',
|
||||
],
|
||||
'issue 4242 repeat unquoted at signs' => [
|
||||
'xy xy xy',
|
||||
'xy',
|
||||
'@ @ @',
|
||||
],
|
||||
'issue 4242 quotes in format and text' => [
|
||||
'Text: "Hooray" for me',
|
||||
'"Hooray" for me',
|
||||
'"Text: "' . NumberFormat::FORMAT_TEXT,
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user