';$c->endflag = '';$c->init();$c->regexp = |(.*)
(.*)
|uis;$c->parse();print_rr($c->result);*/
复制代码
php文本采集类文件:
/**
模块名:php文本采集类**/class collection{//入口 公有var $url; //欲分析的url地址var $content; //读取到的内容var $regexp; //要获取部分的正则表达式 var $codefrom; //原文的编码var $codeto; //欲转换的编码var $timeout; //采集等待的时间var $startflag; //文章开始采集的标志 默认为0 在进行采集条目时,只对$startflag 和 $endflag之间的文字块进行搜索和采集。
var $endflag; //文章结束采集的标志 默认为文章末尾 在进行采集条目时,只对$startflag 和 $endflag之间的文字块进行搜索和采集。 var $block; //$startflag 和 $endflag之间的文字块//出口 私有var $result; //输出结果//初始化收集器
function init(){ if(empty($url)) $this->getfile(); $this->convertencoding();}//采集所需内容function parse(){ $this->getblock(); preg_match_all($this->regexp, $this->block ,$this->result,preg_set_order); return $this->block;}//错误处理function error($msg){ echo $msg;}//读取远程网页 如果成功,传回文件;如果失败传回falsefunction getfile(){ $datalines = @file($this->url); if(!$datalines){ $this->error(can't read the url:.$this->url); return false; } else { $importdata = implode('', $datalines); $importdata = str_replace(array (\r\n, \r), \n, $importdata); $this->content = $importdata; } } //获取所需要的文字块 function getblock(){ if(!empty($this->startflag)) $this->block = substr($this->content,strpos($this->content,$this->startflag)); if(!empty($this->endflag)) $this->block = substr($this->block,0,strpos($this->block,$this->endflag)); } //内容编码的转换 function convertencoding(){ if(!empty($this->codeto)) $this->codefrom = mb_detect_encoding($this->content); //如果给定转换方案,才执行转换。 if(!empty($this->codeto)) $this->content = mb_convert_encoding($this->content,$this->codeto,$this->codefrom) or $this->error(can't convert encoding); }}//end of class?>
复制代码