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

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

WBOY
Release: 2016-06-06 20:26:09
Original
1418 people have browsed it

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.'
';
}

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template