-5

I use file_get_content to get content of a website. This gets all the content but I only need content in <div class="nomal"> tag in content I get. It have some <div class="nomal"> tag I need get all <div class="nomal"> tag. My function:

function get_need_content($str, $strstart, $strend){
    $new_startstr = strpos($str, $strstart);
    $new_strend = strpos($str, $strend);
    echo $final_str=substr($str, $new_startstr, $new_strend );
}

this function only get one in all <div class="nomal"> tag. I need to get all.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • You can use a DOM parser, like [DOMDocument](http://php.net/manual/en/class.domdocument.php) to get the data you need. – gen_Eric Nov 18 '11 at 15:11

2 Answers2

0

If you are looking for a specific tag in the return from file_get_content, you can either run a preg_match to get the value that you are looking for, or because you say it is a website you can load the site as a SimpleXMLElement or DOMDocument to get the value of a specific tag.

moranjk
  • 151
  • 1
  • 5
0

You're probably better off using SimpleXML with an XPath query to pull all instances of the tag you want:

$str = '
<html>
  <head>
  </head>
  <body>
    <p>one</p>
    <p>two</p>
    <p>three</p>
  </body>
</html>
';

$xml = new SimpleXMLElement($str);
foreach ($xml->xpath('//p') as $item) {
    print_r($item);
}
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98