0

I want to get the text from a div in this page:

<html>
<head>Hello world!</head>
<body>
This is a test!<br>Hello man!<br>
    <div class="special">
    I want this text
    </div>
</body>
</html>

I am using this code to get the content without any tags:

echo strip_tags(file_get_contents('http://website.com'));

However, I would like only to get the content from the

<div class="special">

from that page. Is that possible in PHP?

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
John Balls
  • 11
  • 1
  • 9
  • 1
    That's possible, use an HTML parser. Now go search please, there are tons of examples on SO. – HamZa Jun 21 '13 at 09:15
  • 1
    There should be dozens of dupes of this, the above is just the first I found. Use `DOMDocument` and `DOMXPath`. – Jon Jun 21 '13 at 09:17
  • How? I see those topics but they are too hard to understand. – John Balls Jun 21 '13 at 09:21
  • 2
    @JohnBalls: Too hard? Basic examples are like 5 lines long. But you do need to read the manual you know. – Jon Jun 21 '13 at 09:27
  • The problem is that I am portuguese and I have 13 years old – John Balls Jun 21 '13 at 09:34
  • @JohnBalls: Ah I see. Language is always a problem, unfortunately you need to improve your English because you are missing out on a lot of resources otherwise. In this situation improving English == improving programming skills. I 'm not sure what else to suggest. – Jon Jun 21 '13 at 09:54

1 Answers1

0

Use a HTML parser to parse the object you read using file_get_contents(). This question lists a bunch of parsers you could use

Community
  • 1
  • 1
Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65