Back to Top

To grab content from nested tags in PHP

To grab content from nested tags in PHP

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

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 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.

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)

  1. Nice Tutorial…keep up the good work
    Thanks For Sharing….i m gonna try this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to create Cron jobs in WordPress?

Posted on 5 years ago


The Readers’ Poll – March 2013

Posted on 5 years ago


Getting Started with Google Apps Script

Posted on 2 years ago


How to Count Number of Columns in MySQL

Posted on 6 years ago


The Readers’ Poll – May 2014

Posted on 4 years ago