Search code examples
phpsimple-html-dom

Simple HTML DOM find tags and fetch data from page link


Simple HTML DOM find tags and fetch data from page link Hi I'm Simple HTML DOM, basically i need to get h2 title and the content from the links (page/id/1). The point I'm getting stack is getting data from page . The format should be the same that is

  1. Title

contet form lik1 , content from link5

  1. title 2

content from link , content from 2

   <section class="level">
       <h2> title </h2>
       <a class="links" href="page/id/1">link1 </a>
       <a class="links" href="page/id/2">link2 </a>
       <a class="links" href="page/id/3">link3 </a>
       <a class="links" href="page/id/4">link4 </a>
       <a class="links" href="page/id/5">link5 </a>
   </section>
   <section class="level">
       <h2> title 2 </h2>
       <a class="links" href="page/id/7">link1 </a>
       <a class="links" href="page/id/8">link2 </a>
   </section>
   <section class="level">
       <h2> title 3  </h2>
       <a class="links" href="page/id/9">link2 </a>
       <a class="links" href="page/id/10">link3 </a>
   </section>

I know it should be along these line any help guys

   foreach ($html->find('h2') as $key => $value) { 
       echo $html->find('h2',0)->plaintext;
       //this is where Im stack getting the data from the link 
       foreach ( ) {
           echo data from the link example.com/page.php/id/1 
           echo data from the link example.com/page.php/id/2 
       }      
   }

Solution

  • You could find the <section> with the classname level using find('section[class=level]') Then you could for example loop the childnodes and check the nodeName.

    To get only the anchors, you could use find('section[class=level] a')

    For example:

    $html = new simple_html_dom();
    $html->load($data);
    $result = $html->find('section[class=level]');
    foreach ($result  as $item) {
        foreach($item->childNodes() as $childNode) {
            if ($childNode->nodeName() === "h2") {
                echo $childNode->innertext . "<br>";
            }
            if ($childNode->nodeName() === "a") {
                echo $childNode->getAttribute("href") . "<br>";
            }
        }
    }
    

    Result

     title 
    page/id/1
    page/id/2
    page/id/3
    page/id/4
    page/id/5
     title 2 
    page/id/7
    page/id/8
     title 3  
    page/id/9
    page/id/10