note 101523 added to regexp.reference.recursive
| From: | jonah at nucleussystems dot com | Date: | Wed, 22 Dec 2010 02:18:33 +0000 |
| Subject: | note 101523 added to regexp.reference.recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175134@lists.php.net to get a copy of this message | ||
An unexpected behavior came up that introduced a very hard-to-track bug in some code I was working
on. It has to do with the preg_match_all PREG_OFFSET_CAPTURE flag. When you capture the offset of
a sub-match, it's offset is given _relative_ to it's parent. For example, if you extract
the value between < and > recursively in this string:
<this is a <string>>
You will get an array that looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[0] => <this is a <string>>
[1] => 0
)
[1] => Array
(
[0] => this is a <string>
[1] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => <string>
[1] => 0
)
[1] => Array
(
[0] => string
[1] => 1
)
)
)
Notice that the offset in the last index is one, not the twelve we expected. The best way to solve
this problem is to run over the results with a recursive function, adding the parent's offset.
----
Server IP: 69.147.83.197
Probable Submitter: 208.86.47.254
----
Manual Page -- http://www.php.net/manual/en/regexp.reference.recursive.php
Edit -- https://master.php.net/note/edit/101523
Del: integrated -- https://master.php.net/note/delete/101523/integrated
Del: useless -- https://master.php.net/note/delete/101523/useless
Del: bad code -- https://master.php.net/note/delete/101523/bad+code
Del: spam -- https://master.php.net/note/delete/101523/spam
Del: non-english -- https://master.php.net/note/delete/101523/non-english
Del: in docs -- https://master.php.net/note/delete/101523/in+docs
Del: other reasons-- https://master.php.net/note/delete/101523
Reject -- https://master.php.net/note/reject/101523
Search -- https://master.php.net/manage/user-notes.php