Forum

How do you parse an...
 
Notifications
Clear all

How do you parse and process HTML/XML in PHP and extract information from it?


(@elias)
Illustrious Member
Joined: 5 months ago
Posts: 4277
Topic starter  

How do you parse and process HTML/XML in PHP and extract information from it?


   
Quote
(@elias)
Illustrious Member
Joined: 5 months ago
Posts: 4277
Topic starter  

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('
    Hello
    World

    '); 

  • $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 
  •  
  • // Find all images 
  • foreach($html->find('img') as $element) 
  • echo $element->src . '
    '; 
  •  
  • // Find all links 
  • foreach($html->find('a') as $element) 
  • echo $element->href . '
    '; 

   
ReplyQuote
Share: