Home  >  Article  >  php教程  >  php查找任何页面上的所有链接的方法

php查找任何页面上的所有链接的方法

WBOY
WBOYOriginal
2016-06-06 20:26:091383browse

php查找页面上的所有链接该怎么实现?使用DOM就可以轻松从任何页面上抓取链接,下面有个不错的示例,感兴趣的朋友可以参考下

使用DOM,你可以轻松从任何页面上抓取链接,,代码示例如下:

复制代码 代码如下:


$html = file_get_contents('http://www.example.com');
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'
';
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn