Getting node's text in PHP DOM

42,833

Solution 1

So long as you can affect the DOM, you could remove that span.

$span = $div->getElementsByTagName('span')->item(0);
$div->removeChild($span);

$nodeValue = $div->nodeValue;

Alternatively, just access the text node of $div.

foreach($div->childNodes as $node) {

    if ($node->nodeType != XML_TEXT_NODE) {
        continue;
    }
    $nodeValue = $node;
}

If you end up with more text nodes and only want the first, you can break after the first assignment of $nodeValue.

Solution 2

You can access DOMText node directly using XPath:

$xpath = new DOMXPath($dom_document);
$node = $xpath->query('//div/text()')->item(0);
echo $node->textContent; // text
Share:
42,833
Ben G
Author by

Ben G

Python/Django, Rails, PHP, and JavaScript programmer. Been making websites for a really long time.. interested in learning new stuff all the time.

Updated on June 19, 2020

Comments

  • Ben G
    Ben G about 4 years

    How could I extract the string "text" from this markup using the PHP DOM?

    <div><span>notthis</span>text</div>
    

    $div->nodeValue includes "notthis"