Monkey

bre...">

PHP DOM: get all text values

I have an HTML string that might look something like this:

<body>
  <div>
    <span class="blah">Monkey </span>
    <p>breath really <b>stinks</b></p>
    And I don't like it!
  </div>
</body>

      

As you can see, there is some text contained correctly as a value inside the element, there are elements containing text nodes and other elements. I would like to be able to get all the text values underneath the body (assume body is the DOMElement that I have stored in a variable).

So, the result will look something like this:

Monkey breath really stinks And I don't like it!

How should I do it? XPath? Regexps? Magic?

+3


source to share


1 answer


If you don't mind using jquery, I may have an answer to this question.

First, we need to crawl the content. Use php curl and echo content for this. After you get the content in the body, run the jquery function which has the following line,

assuming all text is contained in a div with content id



$('#content').text() gives you the required output.

      

Remember to use the jquery delegate to bind the function to whatever you choose.

0


source







All Articles