Home  >  Article  >  php教程  >  PHP读取某站点的链接的函数的方法

PHP读取某站点的链接的函数的方法

WBOY
WBOYOriginal
2016-06-13 10:31:19987browse


  原理:读取某站点的源文件,再利用正则分析其源代码,得到所有链接
  
  /**********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;$i  if(!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