Recently I am working with the scraping of data from external URL and for scraping we always need to use a preg_match function to get matched data.
I want to fetch all “div” tags and their respective content and nested div’s through PHP function and I have used preg_match, but it’s creating an issue.
For example, I have syntax for HTML tags something like below
1 2 3 4 5 6 7 |
In from above code, I want to get everything within the div class=’xyz’ but preg_match function going to stop executing after getting the first div > tag.
So, I have used DOMDocumen for fetching all nested divs value within div class=’xyz’ using the nodeValue element of DOMDocument but it is not returning HTML tags within the div tag.
Here, I am going to explain how I solve above issue when try to fetch multiple nested divs in PHP so let’s see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function nestedHTML($node) { $doc = $node->ownerDocument; $frag = $doc->createDocumentFragment(); foreach ($node->childNodes as $child) { $frag->appendChild($child->cloneNode(TRUE)); } return $doc->saveXML($frag); } $html = "<div class='xyz'><div class='abc'>contents</div></div>"; libxml_use_internal_errors(true); // if any warning generate $doc = new DOMDocument(); $doc->loadHTML($html); $doc->saveHTML(); $nodes = $doc->getElementsByTagName('div'); foreach ($nodes as $node) { $class = $node->getAttribute('class'); if($class == 'xyz'){ echo nestedHTML($node); } } |
That’s it. you just need to change URL or code require to display.
Thank you for reading.Feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (1)