您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一个php文本采集类

2024/4/16 5:19:05发布17次查看
$c = new collection();$c->url = 'http://bbs.it-home.org/subject_search?cat=1001&search_text=%e5%b9%b4%e8%bd%bb%e4%ba%ba';$c->startflag = '
';$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?>
复制代码
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product