40

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

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

$div->nodeValue includes "notthis"

2 Answers 2

42

You can access DOMText node directly using XPath:

$xpath = new DOMXPath($dom_document);
$node = $xpath->query('//div/text()')->item(0);
echo $node->textContent; // text
Sign up to request clarification or add additional context in comments.

Comments

28

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.