note 46617 deleted from function.preg-match by sean

From: Date: Sun, 17 Oct 2004 23:26:40 +0000
Subject: note 46617 deleted from function.preg-match by sean
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-78707@lists.php.net to get a copy of this message
Note Submitter: caffinated ---- 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" } ---- Reason: useless note

« previous php.notes (#78707) next »