如何获取所有<a> tags from the tag using PHP DOM?

时间:2015-06-17 15:45:37

标签: php html dom curl

I am using PHP's curl for getting webpage data, and for extracting <a> tags from the <body> I am using DOM Document, but it is creating an error.

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
  CURLOPT_URL => "http://www.google.co.in/?gfe_rd=cr&ei=B5GBVezbDeHA8geU8pfYBw",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_USERAGENT => 'Webbot UA'
));
$result = curl_exec($ch);
curl_close($ch);
if (isset($result)){
  $doc = new DomDocument;
  $doc->Load($result);
  var_dump($doc['a']);
}
?>

1 个答案:

答案 0 :(得分:1)

我不会使用DomDocument,使用var1但这只是因为我相信它的执行速度更快,但可能是错误的。

SimpleXMLElement::xpath

要使用DomDocument,请查看$result = $xml->xpath('//a'); while(list( , $node) = each($result)) { echo 'a: ',$node,"\n"; }

DOMDocument::getElementsByTagName
相关问题