Simple HTML DOM - child selectors (CSS)

I am trying to select the (direct) child of a parent div.element using the > combinator, but it does not work.

HTML:

 <div class="element"> <p>test</p> </div> <div class="element"> <div class="selected"> <p>test2</p> </div> </div> 

PHP:

 $html->find('div.element > p', 0); 

I want to select the direct element p .

If it is a nested descendant, it should not return anything, but returns test2 .

How can I write it to return test , but not test2 ? Thanks

UPDATE: The general consensus here regarding SO seems to be that the Simple HTML DOM is bad. I ended up writing my code using PHP DOMDocument , as Phil suggested. I really tested a win-win solution and it really worked. Thanks for the help and happy coding.

+5
source share
1 answer

Well, this should (should, actually :)) work (tested on 4 divs):

 foreach($html->find('div.element') as $element) { $paragraph=$element->find('p',0); if($paragraph==$element->first_child()) echo $paragraph; } 
+2
source

All Articles