本文介绍了php DOMDocument添加了< html>具有DOCTYPE声明的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过DOMDocument类向每个链接添加#b哈希。

I'm adding a #b hash to each link via the DOMDocument class.

        $dom = new DOMDocument();
        $dom->loadHTML($output);

        $a_tags = $dom->getElementsByTagName('a');

        foreach($a_tags as $a)
        {
            $value = $a->getAttribute('href');
            $a->setAttribute('href', $value . '#b');
        }

        return $dom->saveHTML();

效果很好,但是返回的输出包含 DOCTYPE 声明以及< head> < body> 标记。知道为什么会发生这种情况或如何防止这种情况发生吗?

That works fine, however the returned output includes a DOCTYPE declaration and a <head> and <body> tag. Any idea why that happens or how I can prevent that?

推荐答案

这就是通常可以,是的:生成带有Doctype声明,< head> 标记的完整HTML文档,...

That's what DOMDocument::saveHTML() generally does, yes : generate a full HTML Document, with the Doctype declaration, the <head> tag, ...

两个可能的解决方案:


  • 如果您使用的是PHP> = 5.3,则 saveHTML()接受一个可能对您有帮助的附加参数

    • 请参见以获取更多信息。

    • If you are working with PHP >= 5.3, saveHTML() accepts one additional parameter that might help you
      • see The DOM Goodie in PHP 5.3.6 for more informations.

      • 有关示例,请参见。

      这篇关于php DOMDocument添加了&lt; html&gt;具有DOCTYPE声明的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:10
查看更多