phpxpathdomdocumentdomxpath

PHP Domxpath How to get plain text that not inside a tag in an object


So, I have an HTML like this.

<p> 
  Example Text 1
  <span> Text </span>
  Example Text 2
  <img src="example.png">
</p>

I find and save every object inside that p tag like this.

$subObjects = $xpath->evaluate("*", $object);

But I want to get also "Example Text 1" and "Example Text 2" in order like tags. How can I get these plain texts like a tag via using PHP DomXPATH? Thanks in advance.


Solution

  • You need to add the text() function to your XPath query:

    $subObjects = $xpath->evaluate("*|text()", $object);