[英]How to handle special HTML characters in DOMDocument?
Let's say I build an HTML fragment using the following code: 假设我使用以下代码构建HTML片段:
$dom = new DOMDocument();
$header = $dom->createElement("h2", "Lorem & Ipsum");
$dom->appendChild($header);
print($dom->saveHTML());
The raw HTML code printed contains the unescaped &
symbol instead of the necessary HTML &
打印的原始HTML代码包含未转义的
&
符号,而不是必需的HTML &
. 。 The code also throws the following PHP error:
该代码还会引发以下PHP错误:
Warning: DOMDocument::createElement(): unterminated entity reference
警告:DOMDocument :: createElement():未终止的实体引用
What's the best way to handle this? 处理此问题的最佳方法是什么?
It appears that the PHP team is not willing to change this behavior ( source ), so we have to find a workaround instead. PHP团队似乎不愿意更改此行为( source ),因此我们必须找到一种解决方法。
One way is to simply do the encoding yourself in the PHP code, as such: 一种方法是简单地自己在PHP代码中进行编码,如下所示:
$header = $dom->createElement("h2", "Lorem & Ipsum");
However, this isn't always convenient, as the text printed may be inside of a variable or contain other special characters besides &
. 但是,这并不总是很方便,因为打印的文本可能在变量内,或包含
&
以外的其他特殊字符。 So, you can use the htmlentities
function. 因此,您可以使用
htmlentities
函数。
$text = "Lorem & Ipsum";
$header = $dom->createElement("h2", htmlentities($text));
If this still is not an ideal solution, another workaround is to use the textContent
property instead of the second argument in createElement
. 如果这仍然不是理想的解决方案,则另一个解决方法是使用
textContent
属性而不是createElement
中的第二个参数。
In the code below, I've implemented this in a DOMDocument
subclass, so you just have to use the BetterDOM
subclass instead to fix this strange bug. 在下面的代码中,我已经在
DOMDocument
子类中实现了此功能,因此您只需要使用BetterDOM
子类即可解决此奇怪的错误。
class BetterDOM extends DOMDocument {
public function createElement($tag, $text = null) {
$base = parent::createElement($tag);
$base->textContent = $text;
return $base;
}
}
// Correctly prints "<h2>Lorem & Ipsum</h2>" with no errors
$dom = new BetterDOM();
$header = $dom->createElement("h2", "Lorem & Ipsum");
$dom->appendChild($header);
print($dom->saveHTML());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.