• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP远程获取网页内容

php 搞代码 4年前 (2022-01-23) 19次浏览 已收录 0个评论

1.截取php类文件 主要包括 获取url的html内容 然后从html内容中进行正则表达式匹配

	include 'StringBuilder.php';	class CutPage{		function __construct(){		}				//方法一:连接 获取真个文件的文本内容		function getAllContent($url){			$resouce=fopen($url, "r") or die("文件打开失败!");			if(!$resouce){				echo "请求文件不存在!";			}			//$allc//长度太短了取不全用自购建的stringbuilder			$sb=new StringBuilder();						while(!feof($resouce)){				//如果没有到文件的结尾则继续向下执行				$line=fgets($resouce,4096);				$sb->append($line);			}			fclose($resouce);			return $sb->toString();		}		//方法二:获取所有的文本进行文本title的匹配---------格式为:xxxx(xx)xxx的形式表达式			function matchContentTitle($content,$regex_title){			//echo "regex:".$regex_title;			if(preg_match($regex_title, $content)){				$array=preg_split($regex_title, $content,-1,PR

本文来源gao!%daima.com搞$代*!码$网3

搞代gaodaima码EG_SPLIT_DELIM_CAPTURE); return $array[1]; }else{ echo "匹配失败!"; } } //匹配章节 返回携带章节的array function matchContentChapter($content,$regex_chapter){ if(preg_match_all($regex_chapter, $content,$matcher)){ return $matcher[1]; }else{ echo "匹配失败!"; return ; } } }

测试php文件

	include 'CutPage.php';	class Test{		public	static function  start($url,$regex_title,$regex_chapter){				$cut=new CutPage();				$all=$cut->getAllContent($url);				$returnTitle=$cut->matchContentTitle($all, $regex_title);				echo "题目:".$returnTitle;				$arrays=$cut->matchContentChapter($all, $regex_chapter);					for($i=0;$i<count($arrays);$i++){					echo $arrays[$i];				}		}	}	$url="http://www.quanben.com/xiaoshuo/12/12816/";	//正则表达式	$regex_title="/\s*[[(\S+)[[\s*/";			$regex_chapter="/\s*[](\W+)[\s*/";//章			Test::start($url, $regex_title, $regex_chapter);	//echo "cut".$return;	//匹配汉字的正则表达式^[\u4E00-\u9FA5]+		

这里构建了一个类似JAVA中的一个StringBuilder类

class StringBuilder{    const LINE="
"; protected $list= array(''); public function __construct( $str=NULL) { array_push($this->list,$str); } public function append($str) { array_push($this->list,$str); return $this; } public function appendLine($str) { array_push($this->list,$str.self::LINE); return $this; } public function appendFormat( $str,mixed $args) { array_push($this->list, sprintf($str,$args)); return $this; } public function toString() { return implode("",$this->list); } public function __destruct() { unset($this->list); }}

以上就介绍了PHP远程获取网页内容,包括了正则表达式方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP远程获取网页内容
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址