Home >Backend Development >PHP Tutorial >php查找任何页面上的所有链接的方法_PHP

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

WBOY
WBOYOriginal
2016-06-01 11:57:47732browse

使用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