Skip to content

Commit fe3ec55

Browse files
authored
Merge branch 'master' into TextFunctions-New-TextSplit
2 parents 07f4fbe + db2bc3b commit fe3ec55

File tree

21 files changed

+721
-438
lines changed

21 files changed

+721
-438
lines changed

composer.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline.neon

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,51 +2270,6 @@ parameters:
22702270
count: 1
22712271
path: src/PhpSpreadsheet/Shared/Escher/DggContainer/BstoreContainer/BSE.php
22722272

2273-
-
2274-
message: "#^Cannot access offset 0 on array\\|false\\.$#"
2275-
count: 1
2276-
path: src/PhpSpreadsheet/Shared/Font.php
2277-
2278-
-
2279-
message: "#^Cannot access offset 2 on array\\|false\\.$#"
2280-
count: 1
2281-
path: src/PhpSpreadsheet/Shared/Font.php
2282-
2283-
-
2284-
message: "#^Cannot access offset 4 on array\\|false\\.$#"
2285-
count: 1
2286-
path: src/PhpSpreadsheet/Shared/Font.php
2287-
2288-
-
2289-
message: "#^Cannot access offset 6 on array\\|false\\.$#"
2290-
count: 1
2291-
path: src/PhpSpreadsheet/Shared/Font.php
2292-
2293-
-
2294-
message: "#^Parameter \\#1 \\$size of function imagettfbbox expects float, float\\|null given\\.$#"
2295-
count: 1
2296-
path: src/PhpSpreadsheet/Shared/Font.php
2297-
2298-
-
2299-
message: "#^Parameter \\#2 \\$defaultFont of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Drawing\\:\\:pixelsToCellDimension\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null given\\.$#"
2300-
count: 1
2301-
path: src/PhpSpreadsheet/Shared/Font.php
2302-
2303-
-
2304-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Font\\:\\:\\$autoSizeMethods has no type specified\\.$#"
2305-
count: 1
2306-
path: src/PhpSpreadsheet/Shared/Font.php
2307-
2308-
-
2309-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
2310-
count: 1
2311-
path: src/PhpSpreadsheet/Shared/Font.php
2312-
2313-
-
2314-
message: "#^Variable \\$cellText on left side of \\?\\? always exists and is not nullable\\.$#"
2315-
count: 1
2316-
path: src/PhpSpreadsheet/Shared/Font.php
2317-
23182273
-
23192274
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\JAMA\\\\EigenvalueDecomposition\\:\\:\\$cdivi has no type specified\\.$#"
23202275
count: 1

samples/Basic/26_Utf8.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
// at this point, we could do some manipulations with the template, but we skip this step
1313
$helper->write($spreadsheet, __FILE__, ['Xlsx', 'Xls', 'Html']);
1414

15-
if (\PHP_VERSION_ID < 80000) {
16-
// Export to PDF (.pdf)
17-
$helper->log('Write to PDF format');
18-
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf::class);
19-
$helper->write($spreadsheet, __FILE__, ['Pdf']);
20-
}
15+
// Export to PDF (.pdf)
16+
$helper->log('Write to PDF format');
17+
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf::class);
18+
$helper->write($spreadsheet, __FILE__, ['Pdf']);
2119

2220
// Remove first two rows with field headers before exporting to CSV
2321
$helper->log('Removing first two heading rows for CSV export');

samples/Pdf/21b_Pdf.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,20 @@ function replaceBody(string $html): string
3232
$helper->log('Set orientation to landscape');
3333
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
3434

35-
if (\PHP_VERSION_ID < 80000) {
36-
$helper->log('Write to Dompdf');
37-
$writer = new Dompdf($spreadsheet);
38-
$filename = $helper->getFileName('21b_Pdf_dompdf.xlsx', 'pdf');
39-
$writer->setEditHtmlCallback('replaceBody');
40-
$writer->save($filename);
41-
}
35+
$helper->log('Write to Dompdf');
36+
$writer = new Dompdf($spreadsheet);
37+
$filename = $helper->getFileName('21b_Pdf_dompdf.xlsx', 'pdf');
38+
$writer->setEditHtmlCallback('replaceBody');
39+
$writer->save($filename);
4240

4341
$helper->log('Write to Mpdf');
4442
$writer = new Mpdf($spreadsheet);
4543
$filename = $helper->getFileName('21b_Pdf_mpdf.xlsx', 'pdf');
4644
$writer->setEditHtmlCallback('replaceBody');
4745
$writer->save($filename);
4846

49-
if (\PHP_VERSION_ID < 80000) {
50-
$helper->log('Write to Tcpdf');
51-
$writer = new Tcpdf($spreadsheet);
52-
$filename = $helper->getFileName('21b_Pdf_tcpdf.xlsx', 'pdf');
53-
$writer->setEditHtmlCallback('replaceBody');
54-
$writer->save($filename);
55-
}
47+
$helper->log('Write to Tcpdf');
48+
$writer = new Tcpdf($spreadsheet);
49+
$filename = $helper->getFileName('21b_Pdf_tcpdf.xlsx', 'pdf');
50+
$writer->setEditHtmlCallback('replaceBody');
51+
$writer->save($filename);

src/PhpSpreadsheet/Calculation/TextData/Extract.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public static function right($value, $chars = 1)
104104
*
105105
* @param mixed $text the text that you're searching
106106
* Or can be an array of values
107-
* @param ?string $delimiter the text that marks the point before which you want to extract
107+
* @param null|array|string $delimiter the text that marks the point before which you want to extract
108+
* Multiple delimiters can be passed as an array of string values
108109
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
109110
* By default, this is the first instance (1).
110111
* A negative value means start searching from the end of the text string.
@@ -132,7 +133,6 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
132133
}
133134

134135
$text = Helpers::extractString($text ?? '');
135-
$delimiter = Helpers::extractString(Functions::flattenSingleValue($delimiter ?? ''));
136136
$instance = (int) $instance;
137137
$matchMode = (int) $matchMode;
138138
$matchEnd = (int) $matchEnd;
@@ -141,13 +141,14 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
141141
if (is_array($split) === false) {
142142
return $split;
143143
}
144-
if ($delimiter === '') {
144+
if (Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')) === '') {
145145
return ($instance > 0) ? '' : $text;
146146
}
147147

148148
// Adjustment for a match as the first element of the split
149149
$flags = self::matchFlags($matchMode);
150-
$adjust = preg_match('/^' . preg_quote($delimiter) . "\$/{$flags}", $split[0]);
150+
$delimiter = self::buildDelimiter($delimiter);
151+
$adjust = preg_match('/^' . $delimiter . "\$/{$flags}", $split[0]);
151152
$oddReverseAdjustment = count($split) % 2;
152153

153154
$split = ($instance < 0)
@@ -161,7 +162,8 @@ public static function before($text, $delimiter, $instance = 1, $matchMode = 0,
161162
* TEXTAFTER.
162163
*
163164
* @param mixed $text the text that you're searching
164-
* @param ?string $delimiter the text that marks the point before which you want to extract
165+
* @param null|array|string $delimiter the text that marks the point before which you want to extract
166+
* Multiple delimiters can be passed as an array of string values
165167
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
166168
* By default, this is the first instance (1).
167169
* A negative value means start searching from the end of the text string.
@@ -189,7 +191,6 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
189191
}
190192

191193
$text = Helpers::extractString($text ?? '');
192-
$delimiter = Helpers::extractString(Functions::flattenSingleValue($delimiter ?? ''));
193194
$instance = (int) $instance;
194195
$matchMode = (int) $matchMode;
195196
$matchEnd = (int) $matchEnd;
@@ -198,13 +199,14 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
198199
if (is_array($split) === false) {
199200
return $split;
200201
}
201-
if ($delimiter === '') {
202+
if (Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')) === '') {
202203
return ($instance < 0) ? '' : $text;
203204
}
204205

205206
// Adjustment for a match as the first element of the split
206207
$flags = self::matchFlags($matchMode);
207-
$adjust = preg_match('/^' . preg_quote($delimiter) . "\$/{$flags}", $split[0]);
208+
$delimiter = self::buildDelimiter($delimiter);
209+
$adjust = preg_match('/^' . $delimiter . "\$/{$flags}", $split[0]);
208210
$oddReverseAdjustment = count($split) % 2;
209211

210212
$split = ($instance < 0)
@@ -215,21 +217,23 @@ public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $
215217
}
216218

217219
/**
220+
* @param null|array|string $delimiter
218221
* @param int $matchMode
219222
* @param int $matchEnd
220223
* @param mixed $ifNotFound
221224
*
222225
* @return string|string[]
223226
*/
224-
private static function validateTextBeforeAfter(string $text, string $delimiter, int $instance, $matchMode, $matchEnd, $ifNotFound)
227+
private static function validateTextBeforeAfter(string $text, $delimiter, int $instance, $matchMode, $matchEnd, $ifNotFound)
225228
{
226229
$flags = self::matchFlags($matchMode);
230+
$delimiter = self::buildDelimiter($delimiter);
227231

228-
if (preg_match('/' . preg_quote($delimiter) . "/{$flags}", $text) === 0 && $matchEnd === 0) {
232+
if (preg_match('/' . $delimiter . "/{$flags}", $text) === 0 && $matchEnd === 0) {
229233
return $ifNotFound;
230234
}
231235

232-
$split = preg_split('/(' . preg_quote($delimiter) . ")/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
236+
$split = preg_split('/' . $delimiter . "/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
233237
if ($split === false) {
234238
return ExcelError::NA();
235239
}
@@ -247,6 +251,28 @@ private static function validateTextBeforeAfter(string $text, string $delimiter,
247251
return $split;
248252
}
249253

254+
/**
255+
* @param null|array|string $delimiter the text that marks the point before which you want to extract
256+
* Multiple delimiters can be passed as an array of string values
257+
*/
258+
private static function buildDelimiter($delimiter): string
259+
{
260+
if (is_array($delimiter)) {
261+
$delimiter = Functions::flattenArray($delimiter);
262+
$quotedDelimiters = array_map(
263+
function ($delimiter) {
264+
return preg_quote($delimiter ?? '');
265+
},
266+
$delimiter
267+
);
268+
$delimiters = implode('|', $quotedDelimiters);
269+
270+
return '(' . $delimiters . ')';
271+
}
272+
273+
return '(' . preg_quote($delimiter ?? '') . ')';
274+
}
275+
250276
private static function matchFlags(int $matchMode): string
251277
{
252278
return ($matchMode === 0) ? 'mu' : 'miu';

0 commit comments

Comments
 (0)