Skip to content

Commit 4f6d1f7

Browse files
authored
Accommodating Slash with preg_quote - Text Functions (#3582)
PR #3513, developed by @SaidkhojaIftikhor, has been stuck for some time awaiting tests. This is the first of three PRs to replace that one. This accomodates the use of slash as a delimiter in functions TEXTAFTER, TEXTBEFORE, TEXTSPLIT, and NUMBERVALUE. The source changes are very simple. Additional tests exercise all the source changes.
1 parent 9a13f52 commit 4f6d1f7

File tree

7 files changed

+37
-5
lines changed

7 files changed

+37
-5
lines changed

src/PhpSpreadsheet/Calculation/TextData/Extract.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private static function buildDelimiter($delimiter): string
261261
$delimiter = Functions::flattenArray($delimiter);
262262
$quotedDelimiters = array_map(
263263
function ($delimiter) {
264-
return preg_quote($delimiter ?? '');
264+
return preg_quote($delimiter ?? '', '/');
265265
},
266266
$delimiter
267267
);
@@ -270,7 +270,7 @@ function ($delimiter) {
270270
return '(' . $delimiters . ')';
271271
}
272272

273-
return '(' . preg_quote($delimiter ?? '') . ')';
273+
return '(' . preg_quote($delimiter ?? '', '/') . ')';
274274
}
275275

276276
private static function matchFlags(int $matchMode): string

src/PhpSpreadsheet/Calculation/TextData/Format.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public static function NUMBERVALUE($value = '', $decimalSeparator = null, $group
294294
}
295295

296296
if (!is_numeric($value)) {
297-
$decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator) . '/', $value, $matches, PREG_OFFSET_CAPTURE);
297+
$decimalPositions = preg_match_all('/' . preg_quote($decimalSeparator, '/') . '/', $value, $matches, PREG_OFFSET_CAPTURE);
298298
if ($decimalPositions > 1) {
299299
return ExcelError::VALUE();
300300
}

src/PhpSpreadsheet/Calculation/TextData/Text.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private static function buildDelimiter($delimiter): string
193193
if (is_array($delimiter) && count($valueSet) > 1) {
194194
$quotedDelimiters = array_map(
195195
function ($delimiter) {
196-
return preg_quote($delimiter ?? '');
196+
return preg_quote($delimiter ?? '', '/');
197197
},
198198
$valueSet
199199
);
@@ -202,7 +202,7 @@ function ($delimiter) {
202202
return '(' . $delimiters . ')';
203203
}
204204

205-
return '(' . preg_quote(/** @scrutinizer ignore-type */ Functions::flattenSingleValue($delimiter)) . ')';
205+
return '(' . preg_quote(/** @scrutinizer ignore-type */ Functions::flattenSingleValue($delimiter), '/') . ')';
206206
}
207207

208208
private static function matchFlags(bool $matchMode): string

tests/data/Calculation/TextData/NUMBERVALUE.php

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
],
5252
'no arguments' => ['exception'],
5353
'boolean argument' => ['#VALUE!', true],
54+
'slash as group separator' => [1234567.1, '1/234/567.1', '.', '/'],
55+
'slash as decimal separator' => [1234567.1, '1,234,567/1', '/', ','],
5456
'issue 3574 null string treated as 0' => [0, '', ',', ' '],
5557
'issue 3574 one or more spaces treated as 0' => [0, ' ', ',', ' '],
5658
'issue 3574 non-blank numeric string okay' => [2, ' 2 ', ',', ' '],

tests/data/Calculation/TextData/TEXTAFTER.php

+7
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,11 @@
248248
1,
249249
],
250250
],
251+
'slash delimiter' => [
252+
'about/that',
253+
[
254+
'How/about/that',
255+
'/',
256+
],
257+
],
251258
];

tests/data/Calculation/TextData/TEXTBEFORE.php

+7
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,11 @@
240240
1,
241241
],
242242
],
243+
'slash delimiter' => [
244+
'How',
245+
[
246+
'How/about/that',
247+
'/',
248+
],
249+
],
243250
];

tests/data/Calculation/TextData/TEXTSPLIT.php

+16
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,20 @@
104104
'',
105105
],
106106
],
107+
'slash as column delimiter' => [
108+
[['Hello', 'World']],
109+
[
110+
'Hello/World',
111+
'/',
112+
'',
113+
],
114+
],
115+
'slash as row delimiter' => [
116+
[['ho', 'w'], ['about', '#N/A'], ['t', 'hat']],
117+
[
118+
'ho.w/about/t.hat',
119+
'.',
120+
'/',
121+
],
122+
],
107123
];

0 commit comments

Comments
 (0)