note 14986 added to function.preg-match-all.php

From: Date: Thu, 23 Aug 2001 12:58:02 +0000
Subject: note 14986 added to function.preg-match-all.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-17020@lists.php.net to get a copy of this message
Here is a sample that looks weird, even if it works: if (preg_match_all("/\"(.+?)\"/", $pattern, $patternMatches)) { echo ($count = (count($patternMatches[0])))." quote delimited strings:<br>"; for($i = 0; $i <= $count; $i++) { echo $patternMatches[0][$i]."<br>"; } } That code should extract all the strings between " and ", right ? And of course it does :) But... but $patternMatches[0] contains the matched strings including the ", which are out of the pattern in the preg match. That code ran on the string "this is" "a strange" "test" will output: 3 quote delimited strings: "this is" "a strange" "test" Replacing $patternMatches[0] with $patternMatches[1], we get what is expected: 3 quote delimited strings: this is a strange test Even if there is only one parenthesis pair like here. http://www.php.net/manual/en/function.preg-match-all.php

« previous php.notes (#17020) next »