note 80081 added to function.preg-match
| From: | joseph at no-spam dot xylex dot net | Date: | Fri, 28 Dec 2007 08:01:48 +0000 |
| Subject: | note 80081 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134064@lists.php.net to get a copy of this message | ||
A quick example of using named recursion and negative lookaheads for finding the outermost div. You
can use this same idea for any type of nested tags.
<?php
$sample =
"lead in text to capture <div>
outside div text
<div>
inner div text
<div>
deep nested text
</div>
</div>
bottom of outside div text
</div> end of text to capture";
preg_match(
'#^(?P<a>.*?)(?P<b>.?<div((.(?!<div))|(?P>b))*?.</div>)(?P<c>.*?)$#s',
$sample, $matches);
echo "<pre>";
var_dump($matches);
//$matches['a'] == "lead in text to capture"
//$matches['b'] == the outermost <div> and child contents (with a leading space)
//$matches['c'] == " end of text to capture"
?>
----
Server IP: 209.41.74.194
Probable Submitter: 76.104.240.254
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- https://master.php.net/note/edit/80081
Del: integrated -- https://master.php.net/note/delete/80081/integrated
Del: useless -- https://master.php.net/note/delete/80081/useless
Del: bad code -- https://master.php.net/note/delete/80081/bad+code
Del: spam -- https://master.php.net/note/delete/80081/spam
Del: non-english -- https://master.php.net/note/delete/80081/non-english
Del: in docs -- https://master.php.net/note/delete/80081/in+docs
Del: other reasons-- https://master.php.net/note/delete/80081
Reject -- https://master.php.net/note/reject/80081
Search -- https://master.php.net/manage/user-notes.php