Learn to parse HTML with DomDocument | PHPenthusiast

PHOTO EMBED

Mon Jan 11 2021 22:13:38 GMT+0000 (Coordinated Universal Time)

Saved by @jenagade #php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
function stripStyleTags($html='')
{
  $dom = new DOMDocument;                
	
  $dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
	
  $xpath = new DOMXPath($dom); 
	
  // Find any element with the style attribute
  $nodes = $xpath->query('//*[@style]');  
	
  // Loop the elements
  foreach ($nodes as $node)               
  {             
    // Remove style attribute
    $node->removeAttribute('style');
  }
  $html = $dom->saveHTML(); 
	
  return $html;
}
content_copyCOPY

https://phpenthusiast.com/blog/parse-html-with-php-domdocument