Skip to content

Commit ff8f801

Browse files
author
MarkBaker
committed
Use preg_replace() with arrays, where appropriate, because it's more efficient that caling several times
1 parent bf46ff1 commit ff8f801

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/PhpSpreadsheet/Reader/Ods/FormulaTranslator.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ public static function convertToExcelAddressValue(string $openOfficeAddress): st
1313
// Cell range 3-d reference
1414
// As we don't support 3-d ranges, we're just going to take a quick and dirty approach
1515
// and assume that the second worksheet reference is the same as the first
16-
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu', '$1!$2:$4', $excelAddress);
17-
// Cell range reference in another sheet
18-
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', '$1!$2:$3', $excelAddress);
19-
// Cell reference in another sheet
20-
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+)/miu', '$1!$2', $excelAddress);
21-
// Cell range reference
22-
$excelAddress = (string) preg_replace('/\.([^\.]+):\.([^\.]+)/miu', '$1:$2', $excelAddress);
23-
// Simple cell reference
24-
$excelAddress = (string) preg_replace('/\.([^\.]+)/miu', '$1', $excelAddress);
16+
$excelAddress = (string) preg_replace(
17+
[
18+
'/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu',
19+
'/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', // Cell range reference in another sheet
20+
'/\$?([^\.]+)\.([^\.]+)/miu', // Cell reference in another sheet
21+
'/\.([^\.]+):\.([^\.]+)/miu', // Cell range reference
22+
'/\.([^\.]+)/miu', // Simple cell reference
23+
],
24+
[
25+
'$1!$2:$4',
26+
'$1!$2:$3',
27+
'$1!$2',
28+
'$1:$2',
29+
'$1',
30+
],
31+
$excelAddress
32+
);
2533

2634
return $excelAddress;
2735
}
@@ -37,14 +45,21 @@ public static function convertToExcelFormulaValue(string $openOfficeFormula): st
3745
// Only replace in alternate array entries (i.e. non-quoted blocks)
3846
// so that conversion isn't done in string values
3947
if ($tKey = !$tKey) {
40-
// Cell range reference in another sheet
41-
$value = (string) preg_replace('/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', '$1!$2:$3', $value);
42-
// Cell reference in another sheet
43-
$value = (string) preg_replace('/\[\$?([^\.]+)\.([^\.]+)\]/miu', '$1!$2', $value);
44-
// Cell range reference
45-
$value = (string) preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/miu', '$1:$2', $value);
46-
// Simple cell reference
47-
$value = (string) preg_replace('/\[\.([^\.]+)\]/miu', '$1', $value);
48+
$value = (string) preg_replace(
49+
[
50+
'/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference in another sheet
51+
'/\[\$?([^\.]+)\.([^\.]+)\]/miu', // Cell reference in another sheet
52+
'/\[\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference
53+
'/\[\.([^\.]+)\]/miu', // Simple cell reference
54+
],
55+
[
56+
'$1!$2:$3',
57+
'$1!$2',
58+
'$1:$2',
59+
'$1',
60+
],
61+
$value
62+
);
4863
// Convert references to defined names/formulae
4964
$value = str_replace('$$', '', $value);
5065

src/PhpSpreadsheet/Writer/Xls/Parser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ private function convertDefinedName(string $name): string
778778
*/
779779
private function getRefIndex($ext_ref)
780780
{
781-
$ext_ref = (string) preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any.
782-
$ext_ref = (string) preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any.
781+
$ext_ref = (string) preg_replace(["/^'/", "/'$/"], ['', ''], $ext_ref); // Remove leading and trailing ' if any.
783782
$ext_ref = str_replace('\'\'', '\'', $ext_ref); // Replace escaped '' with '
784783

785784
// Check if there is a sheet range eg., Sheet1:Sheet2.

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,7 @@ private function writeUrlExternal($row1, $col1, $row2, $col2, $url): void
10951095

10961096
// Strip URL type and change Unix dir separator to Dos style (if needed)
10971097
//
1098-
$url = (string) preg_replace('/^external:/', '', $url);
1099-
$url = (string) preg_replace('/\//', '\\', $url);
1098+
$url = (string) preg_replace(['/^external:/', '/\//'], ['', '\\'], $url);
11001099

11011100
// Determine if the link is relative or absolute:
11021101
// relative if link contains no dir separator, "somefile.xls"

0 commit comments

Comments
 (0)