php 获取页面中指定内容的实现类


Posted in PHP onJanuary 23, 2014

功能:

1.获取内容中的url,email,image。

2.替换内容中的url,email,image。

url:<a href="url">xxx</a>

email:admin@admin.com

image:<img src="image">

Grep.class.php

<?php 
/** grep class 
* Date: 2013-06-15 
* Author: fdipzone 
* Ver: 1.0 
* 
* Func: 
* 
* set: 设置内容 
* get: 返回指定的内容 
* replace: 返回替换后的内容 
* get_pattern 根据type返回pattern 
*/ class Grep{ // class start 
private $_pattern = array( 
'url' => '/<a.*?href="((http(s)?:\/\/).*?)".*?/si', 
'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/', 
'image' => '/<img.*?src=\"(http:\/\/.+\.(jpg|jpeg|gif|bmp|png))\">/i' 
); 
private $_content = ''; // 源内容 

/* ?置搜?さ?热 
* @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){ 
$type = strtolower($type); 
if($this->_content=='' || !in_array($type, array_keys($this->_pattern))){ 
return array(); 
} 
$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(); 
} 

/* 获取替换后的内容 
* @param String $type 
* @param String $callback 
* @return String 
*/ 
public function replace($type='', $callback=''){ 
$type = strtolower($type); 
if($this->_content=='' || !in_array($type, array_keys($this->_pattern)) || $callback==''){ 
return $this->_content; 
} 
$pattern = $this->get_pattern($type); 
return preg_replace_callback($pattern, $callback, $this->_content); 
} 

/* 根据type获取pattern 
* @param String $type 
* @return String 
*/ 
private function get_pattern($type){ 
return $this->_pattern[$type]; 
} 
} // class end 
?>

Demo
<?php 
header('content-type:text/htm;charset=utf8'); require('Grep.class.php'); 
$content = file_get_contents('http://www.test.com/'); 
$obj = new Grep(); 
$obj->set($content); 
$url = $obj->get('url', 0); 
$email = $obj->get('email', 1); 
$image = $obj->get('image', 1); 
print_r($url); 
print_r($email); 
print_r($image); 
$url_new = $obj->replace('url', 'replace_url'); 
echo $url_new; 
function replace_url($matches){ 
return isset($matches[1])? '[url]'.$matches[1].'[/url]' : ''; 
} 
?>
PHP 相关文章推荐
用Apache反向代理设置对外的WWW和文件服务器
Oct 09 PHP
PHP foreach循环使用详解与实例代码
May 08 PHP
PHP的autoload自动加载机制使用说明
Dec 28 PHP
关于Zend Studio 配色方案插件的介绍
Jun 24 PHP
php和editplus正则表达式去除空白行
Apr 17 PHP
PHP使用array_merge重新排列数组下标的方法
Jul 22 PHP
解决php表单重复提交实现方法
Sep 29 PHP
PHP设计模式之观察者模式实例
Feb 22 PHP
PHP 常用时间函数资料整理
Oct 22 PHP
php实现异步将远程链接上内容(图片或内容)写到本地的方法
Nov 30 PHP
php中照片旋转 (orientation) 问题的正确处理
Feb 16 PHP
PHP 多进程与信号中断实现多任务常驻内存管理实例方法
Oct 04 PHP
php 根据url自动生成缩略图并处理高并发问题
Jan 23 #PHP
php 字符串压缩方法比较示例
Jan 23 #PHP
php 生成短网址原理及代码
Jan 23 #PHP
解决php接收shell返回的结果中文乱码问题
Jan 23 #PHP
php弹出对话框实现重定向代码
Jan 23 #PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
Jan 22 #PHP
简单的php缓存类分享     php缓存机制
Jan 22 #PHP
You might like
php+javascript的日历控件
2009/11/19 PHP
PHP读取大文件的类SplFileObject使用介绍
2014/04/09 PHP
php调用mysql存储过程实例分析
2014/12/29 PHP
PHP使用JSON和将json还原成数组
2015/02/12 PHP
php 使用array函数实现分页
2015/02/13 PHP
php header函数的常用http头设置
2015/06/25 PHP
浅谈PHP中的面向对象OOP中的魔术方法
2017/06/12 PHP
一组JS创建和操作表格的函数集合
2009/05/07 Javascript
js确定对象类型方法
2012/03/30 Javascript
jQuery去掉字符串起始和结尾的空格(多种方法实现)
2013/04/01 Javascript
详解vue组件化开发-vuex状态管理库
2017/04/10 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
ES2020 已定稿,真实场景案例分析
2020/05/25 Javascript
一分钟学会JavaScript中的try-catch
2020/12/14 Javascript
Java多线程编程中ThreadLocal类的用法及深入
2016/06/21 Python
获取python的list中含有重复值的index方法
2018/06/27 Python
对python 中class与变量的使用方法详解
2019/06/26 Python
使用opencv中匹配点对的坐标提取方式
2020/06/04 Python
Python3爬虫关于识别检验滑动验证码的实例
2020/07/30 Python
英国领先的男士美容护发用品公司:Mankind
2016/08/31 全球购物
美国最古老的精致书写工具制造商:A.T. Cross(高仕)
2018/01/30 全球购物
什么是设计模式
2012/06/17 面试题
医学专业个人求职自荐信格式
2013/09/23 职场文书
网游商务专员求职信
2013/10/15 职场文书
考博专家推荐信模板
2013/12/02 职场文书
高中军训感言200字
2014/02/23 职场文书
爱护草坪标语
2014/06/24 职场文书
垃圾桶标语
2014/06/24 职场文书
社会工作专业自荐信
2014/09/26 职场文书
判缓刑人员个人思想汇报
2014/10/10 职场文书
中学生打架检讨书
2014/10/13 职场文书
2016年春季开学典礼新闻稿
2015/11/25 职场文书
初中地理教学反思
2016/02/19 职场文书
委托书范本格式
2019/04/18 职场文书
如何制定一份可行的计划!
2019/06/21 职场文书
【DOTA2】高能暴走TK秀!PSG LGD vs ASTER - DPC 2022 WINTER TOUR CN
2022/04/02 DOTA