note 80081 deleted from function.preg-match by tularis
| From: | tularis@php.net | Date: | Thu, 03 Jul 2008 21:37:12 +0000 |
| Subject: | note 80081 deleted from function.preg-match by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-141140@lists.php.net to get a copy of this message | ||
Note Submitter: joseph at no-spam dot xylex dot net
----
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"
?>