Possible Duplicate:
How to parse and process HTML with PHP?
I wasn't sure how to phrase this question.
Basically I have this php code:
$new_html = preg_replace('!<div.*?id="spotlight".*?>.*?</div>!is', '', $html);
I want this to change html code from this (example, not actual html):
<div id="container">
<div id="spotlight">
<!-- empty -->
</div>
<div id="content">
<!-- lots of content -->
</div>
</div>
To this:
<div id="container">
<div id="content">
<!-- lots of content -->
</div>
</div>
As you can see the php code will do this successfully, because the regex is looking for:
<div{anything}id="spotlight"{anything}>{anything}</div>
However
if the div id="spotlight" contains a child div like so:
<div id="container">
<div id="spotlight">
<div></div>
</div>
<div id="content">
<!-- lots of content -->
</div>
</div>
then the regex will match the end div tag of the child div!
How do i prevent this? How to i tell regex to ignore the closing div if another div was opened?
Thanks
Use DOMDocument
:
$html = '<div id="container">
<div id="spotlight">
<!-- empty -->
</div>
<div id="content">
<!-- lots of content -->
</div>
</div>';
$dom = new DOMDocument;
$dom->loadXML($html);
$xpath = new DOMXPath($dom);
$query = '//div[@id="spotlight"]';
$entries = $xpath->query($query);
foreach($entries as $one){
$one->parentNode->removeChild($one);
}
echo $dom->saveHTML();
$a = preg_replace('/<div[^>]+>\\s+<\/div>/', '', $a);