PHP DOM textContent vs nodeValue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12380919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:27:43  来源:igfitidea点击:

PHP DOM textContent vs nodeValue?

phphtmldom

提问by Xeoncross

PHP DOMnode objects contain a textContentand nodeValueattributes which both seem to be the innerHTML of the node.

PHP DOMnode 对象包含一个textContentnodeValue属性,它们似乎都是节点的innerHTML。

nodeValue: The value of this node, depending on its type

textContent: This attribute returns the text content of this node and its descendants.

nodeValue:此节点的值,取决于其类型

textContent:此属性返回此节点及其后代的文本内容。

What is the difference between these two properties? When is it proper to use one instead of the other?

这两个属性有什么区别?什么时候使用一个而不是另一个合适?

采纳答案by Ja?ck

I finally wanted to know the difference as well, so I dug into the sourceand found the answer; in most cases there will be no discernible difference, but there are a bunch of edge cases you should be aware of.

我终于也想知道有什么区别了,所以我挖了来源,找到了答案;在大多数情况下,不会有明显的区别,但是您应该注意一些边缘情况。

Both ->nodeValueand ->textContentare identical for the following classes (node types):

对于以下类(节点类型),两者->nodeValue->textContent都是相同的:

The ->nodeValueproperty yields NULLfor the following classes (node types):

->nodeValue属性NULL为以下类(节点类型)产生:

The ->textContentproperty is non-existent for the following classes:

->textContent以下类不存在该属性:

  • DOMNameSpaceNode(not documented, but can be found with //namespace:*selector)
  • DOMNameSpaceNode(未记录,但可以通过//namespace:*选择器找到)

The ->nodeValueproperty is non-existent for the following classes:

->nodeValue以下类不存在该属性:

See also: dom_node_node_value_read()and dom_node_text_content_read()

另见:dom_node_node_value_read()dom_node_text_content_read()

回答by mikespook

Hope this will make sense:

希望这会有意义:

$doc = DOMDocument::loadXML('<body><!-- test --><node attr="test1">old content<h1>test</h1></node></body>');
var_dump($doc->textContent);
var_dump($doc->nodeValue);
var_dump($doc->firstChild->textContent);
var_dump($doc->firstChild->nodeValue);

Output:

输出:

string(15) "old contenttest"
NULL
string(15) "old contenttest"
string(15) "old contenttest"

Because: nodeValue - The value of this node, depending on its type

因为: nodeValue - 这个节点的值,取决于它的类型

回答by Adam Leggett

Both textContentand nodeValuereturn unescaped text; i.e. &lt;becomes <.

双方textContentnodeValue返回转义文本; 即&lt;变成<.

textContentconcatenates together all of the content of all children. This is an important distinction; for example, in Chrome the maximum length of nodeValueis 65536 characters (not bytes); if you have already set the content of a node to something longer than that you will need to iterate child nodes if you want to use nodeValuewhereas textContentwill perform the concatenation for you.

textContent将所有子项的所有内容连接在一起。这是一个重要的区别; 例如,在 Chrome 中,最大长度nodeValue为 65536 个字符(不是字节);如果您已经将节点的内容设置为比该内容更长的内容,如果您想使用,nodeValue则需要迭代子节点,而textContent将为您执行连接。

As discussed, there are also several DOM classes that do not support nodeValuebut do support textContent.

如前所述,还有几个 DOM 类不支持nodeValue但支持textContent.

nodeValueis faster for obvious reasons; however don't use it unless you know exactly what the node structure really is.

nodeValue由于显而易见的原因,速度更快;但是,除非您确切地知道节点结构是什么,否则不要使用它。

回答by Valerio Bozz

If you want to assigning a value to the textContentproperty note that it doesn't work for PHP < 5.6.1. Consider using nodeValueinstead, for backward compatibility.

如果您想为textContent属性赋值,请注意它不适用于PHP < 5.6.1nodeValue为了向后兼容,请考虑使用。

回答by pguardiario

They're the same thing. (mikespook's NULL is from a non-DOMNode)

它们是同一回事。(mikespook 的 NULL 来自非 DOMNode)