想象一下这个HTML:
<html>
<head><title>Nice page</title></head>
<body>Hello World <a href=http://google.com>This is a link</a>
<br />
<a href=http://www.google.com> this also
<img src=wrong.image> and here
</a>
</body>
</html>
当我尝试将链接的所有文本用大写字母表示时,它会删除链接标签的img标签。
<html>
<head><title>Nice page</title></head>
<body>Hello World <a href=http://google.com>THIS IS A LINK</a>
<br />
<a href=http://www.google.com> THIS ALSO AND HERE</a>
</body>
</html>
以下是我使用的PHP:
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("index.html");
$elements = $doc->getElementsByTagName("a");
foreach($elements as $elem)
{
$elem->nodeValue = strtoupper($elem->nodeValue);
}
echo $doc->saveHTML();
我如何保护孩子们?