html - PHP & DOM: How can I search through a single element using class names? -


i'm trying search through series of html elements , extract text in divs (based on class name), seem unable search through single element, nodes.

<html> <div class=parent>     <div videoid=1></div>     <div class=inner>testing         <div class=title>test</div>         <div class=date>test</div>         <div class=time>test</div>     </div> </div>  <div class=parent>     <div videoid=2></div>     <div class=inner>testing         <div class=title>test</div>         <div class=date>test</div>         <div class=time>test</div>     </div> </div>  <div class=parent>     <div videoid=3></div>     <div class=inner>testing         <div class=title>test</div>         <div class=date>test</div>         <div class=time>test</div>     </div> </div> </html> $url = new domdocument; $url->loadhtmlfile("text.html");  $finder = new domxpath($url); $classname="parent"; $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); $count = 0; foreach($nodes $element) { //extracts each instance of parent div it's own element.  //within parent div extract value videoid attribute within following child div belonging following attribute: videoid;  //within parent div extract text within following child div belonging following class: title;  //within parent div extract text within following child div belonging following class: date;  //within parent div extract text within following child div belonging following class: time; } 

while there 1 instance of each of child elements within each parent, may in order in parent div, , own children. i'm looking sort of recursive search think?

from parent (elements) got, can continue searching values needed. ->query(expression, context node) has second parameter wherein can put context node need search.

rough example:

// each found parent node foreach($parents $parent) {     $id = $finder->query('./div[@class="id"]', $parent)->item(0)->nodevalue;     // create query                     ^ using found parent context node } 

so in applying those:

$finder = new domxpath($url); $classname = "parent"; $parents = $finder->query("//div[@class='$classname']"); if($parents->length > 0) {     foreach($parents $parent) {         $id = $finder->query('./div[@class="id"]', $parent)->item(0)->nodevalue;         $title = $id = $finder->query('./div[@class="inner"]/div[@class="title"]', $parent)->item(0)->nodevalue;         $date = $id = $finder->query('./div[@class="inner"]/div[@class="date"]', $parent)->item(0)->nodevalue;         $time = $id = $finder->query('./div[@class="inner"]/div[@class="time"]', $parent)->item(0)->nodevalue;          echo $id, '<br/>', $title, '<br/>', $date, '<br/>', $time, '<hr/>';     } } 

sample output

thats case when expect structure always. can search inside parent query , first 1 found, if markup flexible:

foreach($parents $parent) {     $title = $finder->evaluate('string(.//*[@class="title"][1])', $parent);     echo $title, '<br/>'; } 

sample output


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -