php - Extract only first level paragraphs from html -
i have following html:
<div id="myid"> <p>i want this</p> <p>and want this</p> <div> <p>i don't want this</p> </div> </div>
i want extract first level <p>...</p>
elements.
i've tried using excellent simple_html_dom
library e.g. $html->find('#myid p')
in case above, finds 3 <p>...</p>
elements
is there better way this?
instead of having use external library why don't use built in classes handle dom?
first create domdocument instance using html:
$dom = new domdocument(); $dom->loadhtml($yourhtml);
after use domxpath select elements:
$xpath = new domxpath($dom); $nodes = $xpath->query("//*[@id='myid']/p"); var_dump($nodes->length); // outputs 2
this selects p
elements direct children of element id myid
. demo
Comments
Post a Comment