]> granicus.if.org Git - php/commitdiff
Implemented FR #53238 (Make third parameter of preg_match_all optional).
authorAdam Harvey <aharvey@php.net>
Fri, 5 Nov 2010 04:37:27 +0000 (04:37 +0000)
committerAdam Harvey <aharvey@php.net>
Fri, 5 Nov 2010 04:37:27 +0000 (04:37 +0000)
NEWS
UPGRADING
ext/pcre/php_pcre.c
ext/pcre/tests/002.phpt
ext/pcre/tests/preg_match_all_basic.phpt
ext/pcre/tests/preg_match_all_error.phpt

diff --git a/NEWS b/NEWS
index 47d2f6dfc37e67e02730097f13476a40d479bc25..abf454a5dc04e16ad731b5c5434e86f0824a1ced 100644 (file)
--- a/NEWS
+++ b/NEWS
 
 - Deprecated mysql_list_dbs() (Request #50667). (Andrey)
 
+- Implemented FR #53238 (Make third parameter of preg_match_all optional).
+  (Adam)
 - Implemented FR #52555 (Ability to get HTTP response code). (Paul Dragoonis)
 - Implemented FR #51295 (SQLite3::busyTimeout not existing). (Mark)
 - Implemented FR #49366 (Make slash escaping optional in json_encode()). (Adam)
index abaf7baf524cd51beaf0e905518859600dd16c0a..336f37f0389ce58affef40085132e7b50928a71f 100755 (executable)
--- a/UPGRADING
+++ b/UPGRADING
@@ -133,6 +133,9 @@ UPGRADE NOTES - PHP X.Y
   behavior follows the recommendations of Unicode Technical Report #36.
 - htmlspecialchars_decode/html_entity_decode now decode &apos; if the document
   type is ENT_XML1, ENT_XHTML, or ENT_HTML5.
+- The third parameter ($matches) to preg_match_all() is now optional. If
+  omitted, the function will simply return the number of times the pattern was
+  matched in the subject and will have no other side effects.
  
 
 ===================================
index ccb0a51c0ec840e9767c1e3c5f3eab4f38c535df..ce0549fb73d3ad55ec1bac05b1a34fa32cb1f1bf 100644 (file)
@@ -506,7 +506,7 @@ static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global) /* {{{ *
        long                      flags = 0;            /* Match control flags */
        long                      start_offset = 0;     /* Where the new search starts */
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, ((global) ? "ssz|ll" : "ss|zll"), &regex, &regex_len,
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|zll", &regex, &regex_len,
                                                          &subject, &subject_len, &subpats, &flags, &start_offset) == FAILURE) {
                RETURN_FALSE;
        }
@@ -608,7 +608,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
        offsets = (int *)safe_emalloc(size_offsets, sizeof(int), 0);
 
        /* Allocate match sets array and initialize the values. */
-       if (global && subpats_order == PREG_PATTERN_ORDER) {
+       if (global && subpats && subpats_order == PREG_PATTERN_ORDER) {
                match_sets = (zval **)safe_emalloc(num_subpats, sizeof(zval *), 0);
                for (i=0; i<num_subpats; i++) {
                        ALLOC_ZVAL(match_sets[i]);
@@ -649,7 +649,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
                                }
 
                                if (global) {   /* global pattern matching */
-                                       if (subpats_order == PREG_PATTERN_ORDER) {
+                                       if (subpats && subpats_order == PREG_PATTERN_ORDER) {
                                                /* For each subpattern, insert it into the appropriate array. */
                                                for (i = 0; i < count; i++) {
                                                        if (offset_capture) {
@@ -739,7 +739,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
        } while (global);
 
        /* Add the match sets to the output array and clean up */
-       if (global && subpats_order == PREG_PATTERN_ORDER) {
+       if (global && subpats && subpats_order == PREG_PATTERN_ORDER) {
                for (i = 0; i < num_subpats; i++) {
                        if (subpat_names[i]) {
                                zend_hash_update(Z_ARRVAL_P(subpats), subpat_names[i],
@@ -766,7 +766,7 @@ static PHP_FUNCTION(preg_match)
 }
 /* }}} */
 
-/* {{{ proto int preg_match_all(string pattern, string subject, array &subpatterns [, int flags [, int offset]])
+/* {{{ proto int preg_match_all(string pattern, string subject, [array &subpatterns [, int flags [, int offset]]])
    Perform a Perl-style global regular expression match */
 static PHP_FUNCTION(preg_match_all)
 {
index 726390c77e769de79ce8dd2ba76ed562a56a13bb..00f68f365104c589cbe4059c8f09060c7a93fd32 100644 (file)
@@ -20,7 +20,7 @@ var_dump(preg_replace('/(.)/e', 'for ($', 'abc'));
 Warning: preg_match() expects at least 2 parameters, 0 given in %s002.php on line 3
 bool(false)
 
-Warning: preg_match_all() expects at least 3 parameters, 0 given in %s002.php on line 4
+Warning: preg_match_all() expects at least 2 parameters, 0 given in %s002.php on line 4
 bool(false)
 
 Warning: preg_match_all(): Invalid flags specified in %s002.php on line 5
index 06dec1cb452f651e2e2efd35dd1f493cb402b24e..20c4a96be487e053cb40bb3086e6800ef24a0e92 100644 (file)
@@ -3,7 +3,7 @@ Test preg_match_all() function : basic functionality
 --FILE--
 <?php
 /*
-* proto int preg_match_all(string pattern, string subject, array subpatterns [, int flags [, int offset]])
+* proto int preg_match_all(string pattern, string subject, [array subpatterns [, int flags [, int offset]]])
 * Function is implemented in ext/pcre/php_pcre.c
 */
 $string = 'Hello, world! This is a test. This is another test. \[4]. 34534 string.';
@@ -17,6 +17,12 @@ var_dump(preg_match_all('/\d{2}$/', $string, $match4)); //tries to find 2 digits
 var_dump($match4);
 var_dump(preg_match_all('/(This is a ){2}(.*)\stest/', $string, $match5)); //tries to find "This is aThis is a [...] test" (0 matches)
 var_dump($match5);
+
+// Test not passing in a subpatterns array.
+var_dump(preg_match_all('/test/', $string));
+var_dump(preg_match_all('/this isn\'t in the string/', $string));
+var_dump(preg_match_all('/world/', $string));
+var_dump(preg_match_all('/[0-9]/', $string));
 ?>
 --EXPECTF--
 int(1)
@@ -80,3 +86,7 @@ array(3) {
   array(0) {
   }
 }
+int(2)
+int(0)
+int(1)
+int(6)
index 2371ccfef1a91ab5c1ce599b7d89d2f27e0915b9..f6e89284bb532e5f4386ba292b8ad561e430da2b 100644 (file)
@@ -21,8 +21,7 @@ var_dump(preg_match_all($pattern, $subject, $matches, $flags, $offset, $extra_ar
 // Testing preg_match_all withone less than the expected number of arguments
 echo "\n-- Testing preg_match_all() function with less than expected no. of arguments --\n";
 $pattern = '/\w/';
-$subject = 'string_val';
-var_dump(preg_match_all($pattern, $subject));
+var_dump(preg_match_all($pattern));
 echo "Done"
 ?>
 --EXPECTF--
@@ -30,7 +29,7 @@ echo "Done"
 
 -- Testing preg_match_all() function with Zero arguments --
 
-Warning: preg_match_all() expects at least 3 parameters, 0 given in %spreg_match_all_error.php on line %d
+Warning: preg_match_all() expects at least 2 parameters, 0 given in %spreg_match_all_error.php on line %d
 bool(false)
 
 -- Testing preg_match_all() function with more than expected no. of arguments --
@@ -40,6 +39,6 @@ bool(false)
 
 -- Testing preg_match_all() function with less than expected no. of arguments --
 
-Warning: preg_match_all() expects at least 3 parameters, 2 given in %spreg_match_all_error.php on line %d
+Warning: preg_match_all() expects at least 2 parameters, 1 given in %spreg_match_all_error.php on line %d
 bool(false)
 Done