Notifications
Clear all
Topic starter 27/10/2024 6:30 pm
How do you parse and process HTML/XML in PHP and extract information from it?
Topic starter 27/10/2024 6:30 pm
see the article/answerHow do you parse and process HTML/XML in PHP?How can one parse HTML/XML and extract information from it? https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php How to modify HTML elements:// Create DOM from string
- $html = str_get_html('HelloWorld
');
- $html->find('div', 1)->class = 'bar';
- $html->find('div[id=hello]', 0)->innertext = 'foo';
- echo $html;
[list=1]
Extract content from HTML:// Dump contents (without tags) from HTML echo file_get_html('http://www.google.com/')->plaintext;
[list=1]
How to get HTML elements:
- // Create DOM from URL or file
- $html = file_get_html(' http://www.example.com /');
- // Find all images
- foreach($html->find('img') as $element)
- echo $element->src . '
';
- // Find all links
- foreach($html->find('a') as $element)
- echo $element->href . '
';