Skip to content

Commit e809254

Browse files
mvorisekdevnexen
authored andcommitted
Fix GH-8924 str_split of empty string must return empty array
Closes #8945.
1 parent 4df3dd7 commit e809254

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ PHP NEWS
1313
- FPM:
1414
. Added listen.setfib pool option to set route FIB on FreeBSD. (David Carlier)
1515

16+
- Standard:
17+
. Fixed empty array returned by str_split on empty input. (Michael Vorisek)
18+
1619
07 Jul 2022, PHP 8.2.0alpha3
1720

1821
- Core:

UPGRADING

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ PHP 8.2 UPGRADE NOTES
4545
stripos, strripos, lcfirst, ucfirst, ucwords, str_ireplace,
4646
array_change_key_case and sorting with SORT_FLAG_CASE use ASCII case
4747
conversion.
48+
. str_split no longer returns an empty array on empty string.
4849

4950
- SPL:
5051
. The following methods now enforce their signature:

ext/standard/string.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -5885,7 +5885,11 @@ PHP_FUNCTION(str_split)
58855885
RETURN_THROWS();
58865886
}
58875887

5888-
if (0 == ZSTR_LEN(str) || (size_t)split_length >= ZSTR_LEN(str)) {
5888+
if ((size_t)split_length >= ZSTR_LEN(str)) {
5889+
if (0 == ZSTR_LEN(str)) {
5890+
RETURN_EMPTY_ARRAY();
5891+
}
5892+
58895893
array_init_size(return_value, 1);
58905894
add_next_index_stringl(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
58915895
return;

ext/standard/tests/strings/str_split_basic.phpt

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ var_dump( str_split($str,$split_length) );
1616
echo "-- With split_length as default argument --\n";
1717
var_dump( str_split($str) );
1818

19-
echo "Done"
19+
echo "-- Empty string must always return empty array --\n";
20+
var_dump( str_split('') );
21+
var_dump( str_split('', 1) );
22+
var_dump( str_split('', 100) );
23+
2024
?>
2125
--EXPECT--
2226
*** Testing str_split() : basic functionality ***
@@ -80,4 +84,10 @@ array(22) {
8084
[21]=>
8185
string(1) "e"
8286
}
83-
Done
87+
-- Empty string must always return empty array --
88+
array(0) {
89+
}
90+
array(0) {
91+
}
92+
array(0) {
93+
}

ext/standard/tests/strings/str_split_variation3.phpt

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ echo "Done"
3939
--EXPECTF--
4040
*** Testing str_split() : double quoted strings for 'str' ***
4141
-- Iteration 1 --
42-
array(1) {
43-
[0]=>
44-
string(0) ""
42+
array(0) {
4543
}
4644
-- Iteration 2 --
4745
array(1) {

ext/standard/tests/strings/str_split_variation4.phpt

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ echo "Done"
3838
--EXPECT--
3939
*** Testing str_split() : single quoted strings for 'str' ***
4040
-- Iteration 1 --
41-
array(1) {
42-
[0]=>
43-
string(0) ""
41+
array(0) {
4442
}
4543
-- Iteration 2 --
4644
array(1) {

ext/standard/tests/strings/str_split_variation5.phpt

+2-6
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ echo "Done"
8181
--EXPECT--
8282
*** Testing str_split() : heredoc strings as 'str' argument ***
8383
-- Iteration 1 --
84-
array(1) {
85-
[0]=>
86-
string(0) ""
84+
array(0) {
8785
}
8886
-- Iteration 2 --
89-
array(1) {
90-
[0]=>
91-
string(0) ""
87+
array(0) {
9288
}
9389
-- Iteration 3 --
9490
array(1) {

0 commit comments

Comments
 (0)