Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP8.2 str_split function returns empty arrays for empty strings #3341

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Ignore ignoredErrors when not applicable. [Issue #4375](https://github.com/PHPOffice/PhpSpreadsheet/issues/4375) [PR #4377](https://github.com/PHPOffice/PhpSpreadsheet/pull/4377)
- 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)
- 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)
- Prefer mb_str_split to str_split. [PR #3341](https://github.com/PHPOffice/PhpSpreadsheet/pull/3341)

## 2025-02-08 - 4.0.0

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function toDecimal($value)
}

$binX = '';
foreach (str_split($value) as $char) {
foreach (mb_str_split($value, 1, 'UTF-8') as $char) {
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
}
if (strlen($binX) == 40 && $binX[0] == '1') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function toDecimal($value)
}

$binX = '';
foreach (str_split($value) as $char) {
foreach (mb_str_split($value, 1, 'UTF-8') as $char) {
$binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
}
if (strlen($binX) == 30 && $binX[0] == '1') {
Expand Down
7 changes: 5 additions & 2 deletions src/PhpSpreadsheet/Calculation/MathTrig/Arabic.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ public static function evaluate(mixed $roman): array|int|string
// Convert the roman numeral to an arabic number
$negativeNumber = $roman[0] === '-';
if ($negativeNumber) {
$roman = substr($roman, 1);
$roman = trim(substr($roman, 1));
if ($roman === '') {
return ExcelError::NAN();
}
}

try {
$arabic = self::calculateArabic(str_split($roman));
$arabic = self::calculateArabic(mb_str_split($roman, 1, 'UTF-8'));
} catch (Exception) {
return ExcelError::VALUE(); // Invalid character detected
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Csv/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function countPotentialDelimiters(): void

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

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Security/XmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function findCharSet(string $xml): string
public function scan($xml): string
{
// Don't rely purely on libxml_disable_entity_loader()
$pattern = '/\0*' . implode('\0*', str_split($this->pattern)) . '\0*/';
$pattern = '/\0*' . implode('\0*', mb_str_split($this->pattern, 1, 'UTF-8')) . '\0*/';

$xml = "$xml";
if (preg_match($pattern, $xml)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(?string $id = null, string $cfRule = self::CONDITION

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

foreach ($chars as $i => $char) {
if ($char === 'x') {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,6 @@ private static function setLowercaseCallback(array $matches): string

private static function escapeQuotesCallback(array $matches): string
{
return '\\' . implode('\\', str_split($matches[1]));
return '\\' . implode('\\', mb_str_split($matches[1], 1, 'UTF-8'));
}
}
1 change: 1 addition & 0 deletions tests/data/Calculation/Engineering/HEX2DEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
['4886718345', '123456789'],
[ExcelError::NAN(), '123.45'],
['0', '0'],
['0', ''],
[ExcelError::NAN(), 'G3579A'],
[ExcelError::VALUE(), true],
[ExcelError::VALUE(), false],
Expand Down
1 change: 1 addition & 0 deletions tests/data/Calculation/Engineering/OCT2DEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[ExcelError::NAN(), '3579'],
['44', '54'],
['-165', '7777777533'], // 2's Complement
['0', ''],
[ExcelError::NAN(), '37777777770'], // too many digits
['536870911', '3777777777'], // highest positive
['-536870912', '4000000000'], // lowest negative
Expand Down
8 changes: 8 additions & 0 deletions tests/data/Calculation/MathTrig/ARABIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@
'#VALUE!',
'WRONG',
],
[
0,
'',
],
[
'#NUM!',
'-',
],
];
22 changes: 8 additions & 14 deletions tests/data/Style/NumberFormatDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,32 @@
22269.0625,
'dd-mm-yyyy hh:mm:ss',
],
// Oasis uses upper-case
[
'Oasis uses upper-case' => [
'12/19/1960 01:30:00',
22269.0625,
'MM/DD/YYYY HH:MM:SS',
],
// Date with plaintext escaped with a \
[
'plaintext escaped with backslash' => [
'1960-12-19T01:30:00',
22269.0625,
'yyyy-mm-dd\Thh:mm:ss',
],
// Date with plaintext in quotes
[
'plaintext in quotes' => [
'1960-12-19T01:30:00 Z',
22269.0625,
'yyyy-mm-dd"T"hh:mm:ss \Z',
],
// Date with quoted formatting characters
[
'quoted formatting characters' => [
'y-m-d 1960-12-19 h:m:s 01:30:00',
22269.0625,
'"y-m-d" yyyy-mm-dd "h:m:s" hh:mm:ss',
],
// Date with quoted formatting characters
[
'y-m-d 1960-12-19 h:m:s 01:30:00',
'quoted formatting non-ascii characters' => [
'§1960-12-19',
22269.0625,
'"y-m-d "yyyy-mm-dd" h:m:s "hh:mm:ss',
'"§"yyyy-mm-dd',
],
// Date with fractional/decimal time
[
'fractional/decimal time' => [
'2023/02/28 0:00:00.000',
44985,
'yyyy/mm/dd\ h:mm:ss.000',
Expand Down