Skip to content

Make parenthesized expressions advance the limit count on preg_split #2860

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

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 4 additions & 4 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2495,10 +2495,6 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
ZVAL_STRINGL(&tmp, last_match, &ZSTR_VAL(subject_str)[offsets[0]]-last_match);
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
}

/* One less left to do */
if (limit_val != -1)
limit_val--;
}

last_match = &ZSTR_VAL(subject_str)[offsets[1]];
Expand All @@ -2520,6 +2516,10 @@ PHPAPI void php_pcre_split_impl(pcre_cache_entry *pce, zend_string *subject_str,
}
}

/* One less left to do */
if (limit_val != -1)
limit_val--;

/* Advance to the position right after the last full match */
start_offset = offsets[1];

Expand Down
64 changes: 64 additions & 0 deletions ext/pcre/tests/split3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
preg_split() - parenthesized expressions advance the limit count
--FILE--
<?php

var_dump(
preg_split("/(.)/", "HAHAHA, VERY FUNNY", 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE),
preg_split("/(.)/", "HAHAHA, VERY FUNNY", 3, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE),
preg_split("/(.)/", "HAHAHA, VERY FUNNY", null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)
);

--EXPECT---
array(2) {
[0]=>
string(1) "H"
[1]=>
string(17) "AHAHA, VERY FUNNY"
}
array(3) {
[0]=>
string(1) "H"
[1]=>
string(1) "A"
[2]=>
string(16) "HAHA, VERY FUNNY"
}
array(18) {
[0]=>
string(1) "H"
[1]=>
string(1) "A"
[2]=>
string(1) "H"
[3]=>
string(1) "A"
[4]=>
string(1) "H"
[5]=>
string(1) "A"
[6]=>
string(1) ","
[7]=>
string(1) " "
[8]=>
string(1) "V"
[9]=>
string(1) "E"
[10]=>
string(1) "R"
[11]=>
string(1) "Y"
[12]=>
string(1) " "
[13]=>
string(1) "F"
[14]=>
string(1) "U"
[15]=>
string(1) "N"
[16]=>
string(1) "N"
[17]=>
string(1) "Y"
}