php - domxpath - Extract li tags from second ul -


i trying extract second ul's li tags following. unfortunately, there no classes or ids in html help

<ul>     <li>some text</li>     <li>some text</li>     <li>some text</li> </ul>  <ul>     <li>some more text</li>     <li>some more text</li>     <li>some more text</li> </ul> 

i have tried (a few things, actually):

    $ul = $xpath->query('//ul')->item(1);     $query = '/li';     $lis = $xpath->evaluate($query, $ul); 

thinking me second ul, , can extract there. me second ul's html, i'm misunderstanding `->evaluate? because li's li's, not second ul.

you can directly access them using xpath:

$xpath->query('//ul[2]/li'); 

example:

$html = <<<eof <ul>     <li>some text</li>     <li>some text</li>     <li>some text</li> </ul>  <ul>     <li>some more text</li>     <li>some more text</li>     <li>some more text</li> </ul> eof;  $doc = new domdocument(); $doc->loadhtml($html);  $selector = new domxpath($doc);  // iterate through them... foreach($selector->query('//ul[2]/li') $li) {     echo $li->nodevalue . php_eol; } 

~


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -