PHP DOM textContent vs nodeValue?

PhpHtmlDom

Php Problem Overview


PHP DOMnode objects contain a textContent and nodeValue attributes which both seem to be the innerHTML of the node.

> nodeValue: The value of this node, depending on its type > > textContent: This attribute returns the text content of this node and its descendants.

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

Php Solutions


Solution 1 - Php

I finally wanted to know the difference as well, so I dug into the source and 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 ->nodeValue and ->textContent are identical for the following classes (node types):

The ->nodeValue property yields NULL for the following classes (node types):

The ->textContent property is non-existent for the following classes:

  • DOMNameSpaceNode (not documented, but can be found with //namespace:* selector)

The ->nodeValue property is non-existent for the following classes:

See also: dom_node_node_value_read() and dom_node_text_content_read()

Solution 2 - Php

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

Solution 3 - Php

Both textContent and nodeValue return unescaped text; i.e. &lt; becomes <.

textContent concatenates together all of the content of all children. This is an important distinction; for example, in Chrome the maximum length of nodeValue is 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 nodeValue whereas textContent will perform the concatenation for you.

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

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

Solution 4 - Php

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

Solution 5 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXeoncrossView Question on Stackoverflow
Solution 1 - PhpJa͢ckView Answer on Stackoverflow
Solution 2 - PhpmikespookView Answer on Stackoverflow
Solution 3 - PhpAdam LeggettView Answer on Stackoverflow
Solution 4 - PhpValerio BozzView Answer on Stackoverflow
Solution 5 - PhppguardiarioView Answer on Stackoverflow