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

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -