Home >Backend Development >PHP Tutorial >PHP读取某站点的链接的函数_PHP

PHP读取某站点的链接的函数_PHP

WBOY
WBOYOriginal
2016-06-01 12:38:25944browse

原理:读取某站点的源文件,再利用正则分析其源代码,得到所有链接

/**********qiushuiwuhen(2002-5-20)***********/
if(empty($url))$url = "http://www.csdn.net/expert/";//设置url
$site=substr($url,0,strpos($url,"/",8));//站点
$base=substr($url,0,strrpos($url,"/")+1);//文件所在目录

$fp = fopen($url, "r" );//打开url
while(!feof($fp))$contents.=fread($fp,1024);//

$pattern="|href=['\"]?([^ '\"]+)['\" ]|U";
preg_match_all($pattern,$contents, $regArr, PREG_SET_ORDER);//匹配所有href=
for($i=0;$iif(!eregi("://",$regArr[$i][1]))//是否是相对路径,即是否还有://
if(substr($regArr[$i][1],0,1)=="/")//是否是站点的根目录
echo "link".($i+1).":".$site.$regArr[$i][1]."
";//根目录
else
echo "link".($i+1).":".$base.$regArr[$i][1]."
";//当前目录
else
echo "link".($i+1).":".$regArr[$i][1]."
";//相对路径
}
fclose($fp);
?>

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