php domdocument read element inner text

I’m new to php so please forgive the simple question:

How do I extract the text from an element?

     <span id="myElement">Some text I want to read</span>

I have this for a start:

      $data = $dom->getElementById("myElement");
      $html = $dom->saveHTML($data);

But then? What is the correct instruction?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

To get the text that an element contains, you need the textContent property:

$text = $data->textContent;

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply