<foo>
a
<bar> b </bar>
</foo>
both $foo->textContent
and $foo->nodeValue
return a b
.
How can I get just a
(the text from the node, without text from any child nodes)
textContent gets the content of all elements, including <script> and <style> elements. In contrast, innerText only shows "human-readable" elements. textContent returns every element in the node. In contrast, innerText is aware of styling and won't return the text of "hidden" elements.
What is textContent in JavaScript. It is a property that allows us to get or set the text content of a node, or an element. Setting the textContent property will remove and replace the child nodes with the new text node.
childNodes returns nodes: Element nodes, text nodes, and comment nodes. Whitespace between elements are also text nodes.
The nodeValue property of the Node interface returns or sets the value of the current node.
This might be helpful. Using what I found here and here
$txt = "";
foreach($foo->childNodes as $node) {
if ($node->nodeType == XML_TEXT_NODE) {
$txt .= $node->nodeValue;
}
}
Use firstChild
:
$foo->firstChild->textContent;
Try this code
$doc = new DOMDocument();
$doc->loadXML('<root><foo>a<bar>b</bar></foo><foo>bar</foo></root>');
$foos = $doc->getElementsByTagName('foo');
foreach($foos as $v){
echo $v->firstChild->wholeText.'<br />';
}
The firstChild
property of DOMNode
returns a DOMText
object as there is a "text node" before <bar>
in first <foo>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With