Having:
$content=
'<div id="parent">
<div class="children">
This is short content
</div>
<div class="children">
This is a very long content even longer than the Short content
</div>
<p>
This is a Short content in a paragraph
</p>
This is a Short content without a html elemnt
</div>';
I can remove nodes using DOMDocument by class
(or id
) like this:
$dom->loadHTML($content);
$xpath = new DOMXpath($dom);
if($divToRemove = $xpath->query('.//div[@class="children"]')->item(0))
$divToRemove->parentNode->removeChild($divToRemove);
$content = $dom->saveHTML();
Using above code, I can remove the first div
from $content
. but How can I remove childs that have a short inner text, for example shorter than 20 characters?
EDIT
I have no idea about the child element. It can be a <div>
or a <p>
or something else.
I want to remove every short-length child of parent <div>
Is there any Xpath
query to select nodes regarding their length?
This is what I wantas output:
$content=
'<div id="parent">
<div class="children">
This is a very long content even longer than the Short content
</div>
</div>';
or a
– Peyman Mohamadpour Jun 02 '14 at 16:59