

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Parse HTML with PHP's HTML DOMDocument
The text inside a <div> tag inside class="text" inside <div> with class="main" can be obtained with the following code −
Example
$html = <<<HTML <div class="main"> <div class="text"> This is text 1 </div> </div> <div class="main"> <div class="text"> This is text 2 </div> </div> HTML; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); XPath queries along with the DOMXPath::query method can be used to return the list of elements that are searched for by the user. $tags = $xpath->query('//div[@class="main"]/div[@class="text"]'); foreach ($tags as $tag) { var_dump(trim($tag->nodeValue)); }
Output
This will produce the following output −
string ‘This is text 1’ (length=14) string ‘This is text 2' (length=14)
- Related Questions & Answers
- How to Parse HTML pages to fetch HTML tables with Python?
- How to parse HTML in android?
- HTML DOM S Object
- How to parse HTML in Android using Kotlin?
- PHP Escaping From HTML
- HTML <s> Tag
- Make JavaScript take HTML input from user, parse and display?
- Disabling Android's chrome pull-down-to-refresh feature with HTML.
- JavaScript to parse and show current time stamp of an HTML audio player.
- How to display XML in HTML in PHP?
- HTML DOM HTML Object
- Ember.js browser support with HTML
- HTML <html> Tag
- How to parse a CSV file using PHP
- Client-side image processing with HTML
Advertisements