Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

html - Php Tidy : remove link and style tags inside body

I must cleanup some HTML code to remove <style> and <link> tags inside the <body> tag. I'm already using PHP Tidy to do some cleanup but I did not found how to remove those tags with PHP Tidy.

Do you have a solution ? Or maybe another markup cleaner PHP class...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't know how to do that with Tidy, but you can use DOM

$dom = new DOMDocument;                    // init new DOMDocument
$dom->loadHTML($html);                     // load HTML into it
$xpath = new DOMXPath($dom);               // create a new XPath
$nodes = $xpath->query('//body/style');    // Find all style elements in body tag
foreach($nodes as $node) {                 // Iterate over found elements
    $node->parentNode->removeChild($node); // Remove complete style node
}
echo $dom->saveHTML();                     // output cleaned HTML

For the <link> elements, adjust the Xpath to //body/link.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...