Skip to content

Commit 027a5c6

Browse files
authored
Merge pull request #3341 from HuongNV13/php82-str-split-function-return
Fix PHP8.2 str_split function returns empty arrays for empty strings
2 parents a936254 + 93169c6 commit 027a5c6

File tree

12 files changed

+30
-22
lines changed

12 files changed

+30
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3636
- Ignore ignoredErrors when not applicable. [Issue #4375](https://github.com/PHPOffice/PhpSpreadsheet/issues/4375) [PR #4377](https://github.com/PHPOffice/PhpSpreadsheet/pull/4377)
3737
- Better handling of defined names on sheets whose titles include apostrophes. [Issue #4356](https://github.com/PHPOffice/PhpSpreadsheet/issues/4356) [Issue #4362](https://github.com/PHPOffice/PhpSpreadsheet/issues/4362) [Issue #4376](https://github.com/PHPOffice/PhpSpreadsheet/issues/4376) [PR #4360](https://github.com/PHPOffice/PhpSpreadsheet/pull/4360)
3838
- Partial solution for removing rows or columns that include edge ranges. [Issue #1449](https://github.com/PHPOffice/PhpSpreadsheet/issues/1449) [PR #3528](https://github.com/PHPOffice/PhpSpreadsheet/pull/3528)
39+
- Prefer mb_str_split to str_split. [PR #3341](https://github.com/PHPOffice/PhpSpreadsheet/pull/3341)
3940

4041
## 2025-02-08 - 4.0.0
4142

src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function toDecimal($value)
9696
}
9797

9898
$binX = '';
99-
foreach (str_split($value) as $char) {
99+
foreach (mb_str_split($value, 1, 'UTF-8') as $char) {
100100
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
101101
}
102102
if (strlen($binX) == 40 && $binX[0] == '1') {

src/PhpSpreadsheet/Calculation/Engineering/ConvertOctal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function toDecimal($value)
9696
}
9797

9898
$binX = '';
99-
foreach (str_split($value) as $char) {
99+
foreach (mb_str_split($value, 1, 'UTF-8') as $char) {
100100
$binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
101101
}
102102
if (strlen($binX) == 30 && $binX[0] == '1') {

src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ public static function evaluate(mixed $roman): array|int|string
7474
// Convert the roman numeral to an arabic number
7575
$negativeNumber = $roman[0] === '-';
7676
if ($negativeNumber) {
77-
$roman = substr($roman, 1);
77+
$roman = trim(substr($roman, 1));
78+
if ($roman === '') {
79+
return ExcelError::NAN();
80+
}
7881
}
7982

8083
try {
81-
$arabic = self::calculateArabic(str_split($roman));
84+
$arabic = self::calculateArabic(mb_str_split($roman, 1, 'UTF-8'));
8285
} catch (Exception) {
8386
return ExcelError::VALUE(); // Invalid character detected
8487
}

src/PhpSpreadsheet/Reader/Csv/Delimiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function countPotentialDelimiters(): void
5555

5656
protected function countDelimiterValues(string $line, array $delimiterKeys): void
5757
{
58-
$splitString = str_split($line, 1);
58+
$splitString = mb_str_split($line, 1, 'UTF-8');
5959
$distribution = array_count_values($splitString);
6060
$countLine = array_intersect_key($distribution, $delimiterKeys);
6161

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private function findCharSet(string $xml): string
8787
public function scan($xml): string
8888
{
8989
// Don't rely purely on libxml_disable_entity_loader()
90-
$pattern = '/\0*' . implode('\0*', str_split($this->pattern)) . '\0*/';
90+
$pattern = '/\0*' . implode('\0*', mb_str_split($this->pattern, 1, 'UTF-8')) . '\0*/';
9191

9292
$xml = "$xml";
9393
if (preg_match($pattern, $xml)) {

src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(?string $id = null, string $cfRule = self::CONDITION
3434

3535
private function generateUuid(): string
3636
{
37-
$chars = str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
37+
$chars = mb_str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', 1, 'UTF-8');
3838

3939
foreach ($chars as $i => $char) {
4040
if ($char === 'x') {

src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,6 @@ private static function setLowercaseCallback(array $matches): string
207207

208208
private static function escapeQuotesCallback(array $matches): string
209209
{
210-
return '\\' . implode('\\', str_split($matches[1]));
210+
return '\\' . implode('\\', mb_str_split($matches[1], 1, 'UTF-8'));
211211
}
212212
}

tests/data/Calculation/Engineering/HEX2DEC.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
['4886718345', '123456789'],
1313
[ExcelError::NAN(), '123.45'],
1414
['0', '0'],
15+
['0', ''],
1516
[ExcelError::NAN(), 'G3579A'],
1617
[ExcelError::VALUE(), true],
1718
[ExcelError::VALUE(), false],

tests/data/Calculation/Engineering/OCT2DEC.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
[ExcelError::NAN(), '3579'],
1616
['44', '54'],
1717
['-165', '7777777533'], // 2's Complement
18+
['0', ''],
1819
[ExcelError::NAN(), '37777777770'], // too many digits
1920
['536870911', '3777777777'], // highest positive
2021
['-536870912', '4000000000'], // lowest negative

0 commit comments

Comments
 (0)