[php fopen]php利用fopen实现简单的网页采集程序

更新时间:2019-05-10    来源:链接特效    手机版     字体:

【www.bbyears.com--链接特效】

 代码如下


/**
* 根据URL采集网页内容
*
* @param string $url 链接地址
* @return string
*/

private function fetchbyurl($url){
$handle = fopen($url, ‘r’);
$content = ”;
while (!feof($handle)){
$content .= fgets($handle, 10000);
}
return $content;
//?$this->utf8_iconv($content):”;
}

/*获取所有匹配的内容
* @param string $str 内容
* @param string $start 起始匹配
* @param string $end 中止匹配
* @return array
*/

private function utf8_iconv($content){
return iconv(‘GBK’, ‘UTF-8′, $content);
}
private function strCutAll($str,$start,$end){
$content = explode($start,$str);
$matchs = array();
$sum = count($content);
for( $i = 1;$i < $sum;$i++ ){
$tmp = explode($end,$content[$i]);
$matchs[] = $tmp[0];
unset($tmp);
}
return $matchs;
}

/*获取第一个匹配的内容
* @param string $str 内容
* @param string $start 起始匹配
* @param string $end 中止匹配
* @return string
*/
private function strCut($str, $start, $end){
$content = strstr( $str, $start );
$content = substr( $content, strlen( $start ), strpos( $content, $end ) - strlen( $start ) );
return $content;
}

测试:

/*采集程序*/header("content-Type: text/html; charset=utf-8"); //$nr = file_get_contents(‘/webback/php/php-yi-ju-hua-hou-men-zhuan’); $nr = $this->fetchbyurl(‘/webback/php/php-yi-ju-hua-hou-men-zhuan’);//推荐,还可以用curl dump($this->strCut($nr,’’,"’));//得到内容。需要进一步过滤用(preg_match_all) dump($this->strCutAll($nr,’’,"’)); 得到标题

本文来源:http://www.bbyears.com/wangyetexiao/50438.html

热门标签

更多>>

本类排行