note 46617 added to function.preg-match

From: Date: Sun, 17 Oct 2004 18:33:36 +0000
Subject: note 46617 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-78701@lists.php.net to get a copy of this message
A very common problem I see people running in to is with repeating captures. This appears to be a caevet of PCRE. Take a string like: "foo1foo2foo3" Many seem to think that a regex like !(foo[0-9])+! will match and capture each copy of 'foo' followed by a number and return them as separate captures in the return array, but this is not the case. What occurs here is that each copy of foo[0-9] is captured, but it overwrites the previous capture, so only 'foo3' is returned. example: <?php $str = 'foo1foo2foo3'; preg_match('!(foo[0-9])+!', $str, $match); var_dump($match); ?> The result would be: array(2) { [0]=> string(12) "foo1foo2foo3" [1]=> string(4) "foo3" } ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46617 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46617&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46617&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78701) next »