php 获取页面中指定内容的实现类
php  /  管理员 发布于 7年前   154
1.获取内容中的url,email,image。 2.替换内容中的url,email,image。 url:xxx email:[email protected] image: Grep.class.php class Grep{ // class start private $_pattern = array( private $_content = ''; // 源内容 $type = strtolower($type); if($this->_content=='' || !in_array($type, array_keys($this->_pattern))){ $pattern = $this->get_pattern($type); // 获取pattern preg_match_all($pattern, $this->_content, $matches); return isset($matches[1])? ( $unique==0? $matches[1] : array_unique($matches[1]) ) : array(); } $type = strtolower($type); if($this->_content=='' || !in_array($type, array_keys($this->_pattern)) || $callback==''){ $pattern = $this->get_pattern($type); return preg_replace_callback($pattern, $callback, $this->_content); } ?> require('Grep.class.php'); $content = file_get_contents('http://www.test.com/'); $obj = new Grep(); $url = $obj->get('url', 0); print_r($url); $url_new = $obj->replace('url', 'replace_url'); function replace_url($matches){
/** grep class
* Date: 2013-06-15
* Author: fdipzone
* Ver: 1.0
*
* Func:
*
* set: 设置内容
* get: 返回指定的内容
* replace: 返回替换后的内容
* get_pattern 根据type返回pattern
*/
'url' => '/
'image' => '//i'
);
/* O置搜さ热
* @param String $content
*/
public function set($content=''){
$this->_content = $content;
}
/* 获取指定内容
* @param String $type
* @param int $unique 0:all 1:unique
* @return Array
*/
public function get($type='', $unique=0){
return array();
}
/* 获取替换后的内容
* @param String $type
* @param String $callback
* @return String
*/
public function replace($type='', $callback=''){
return $this->_content;
}
/* 根据type获取pattern
* @param String $type
* @return String
*/
private function get_pattern($type){
return $this->_pattern[$type];
}
} // class end
Demo
复制代码 代码如下:
header('content-type:text/htm;');
$obj->set($content);
$email = $obj->get('email', 1);
$image = $obj->get('image', 1);
print_r($email);
print_r($image);
echo $url_new;
return isset($matches[1])? '[url]'.$matches[1].'[/url]' : '';
}
?>
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号