Reputation: 602
If I have an HTML page containing <div class="post-inner">
, how can I get the content of this div
with class name post-inner
without any JavaScript? I just want the content of the div
tag using PHP as fast as possible. I have tried this code but I don't know what to do after that:
$html = file_get_contents('http://www.q8ping.com/49352.html');
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$sxml = simplexml_import_dom($doc);
print_r($sxml);
Upvotes: 1
Views: 8700
Reputation: 4620
Try this
$page = file_get_contents('http://www.q8ping.com/49352.html');
preg_match_all('/<div class=\"post-inner(.*?)\">(.*?)<\/div>/s',$page,$vv,PREG_SET_ORDER);
print_r($vv[0][0]);
exit;
Upvotes: 1
Reputation: 5105
You can use XPath to accomplish that. Change your code to this:
$html = file_get_contents('http://www.q8ping.com/49352.html');
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$finder = new DomXPath($doc);
$node = $finder->query("//*[contains(@class, 'post-inner')]");
print_r($doc->saveHTML($node->item(0)));
You might want to check first though if an element was found.
Upvotes: 3