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 相关文章推荐
从C/C++迁移到PHP——判断字符类型的函数
Oct 09 PHP
PHP编程最快明白(第一讲 软件环境和准备工作)
Oct 25 PHP
ThinkPHP令牌验证实例
Jun 18 PHP
PHP实现事件机制实例分析
Jun 26 PHP
php搜索文件程序分享
Oct 30 PHP
PHP实现的蚂蚁爬杆路径算法代码
Dec 03 PHP
thinkphp3.x中cookie方法的用法分析
May 19 PHP
轻松实现php文件上传功能
Feb 17 PHP
PHP文件操作实例总结【文件上传、下载、分页】
Dec 08 PHP
PHP常用函数之获取汉字首字母功能示例
Oct 21 PHP
PHP全局使用Laravel辅助函数dd
Dec 26 PHP
tp5.1 框架数据库高级查询技巧实例总结
May 25 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
微信营销平台系统?刮刮乐的开发
2014/06/10 PHP
功能强大的PHP发邮件类
2016/08/29 PHP
phpstorm 正则匹配删除空行、注释行(替换注释行为空行)
2018/01/21 PHP
WEB高性能开发之疯狂的HTML压缩
2010/06/19 Javascript
Dom 学习总结以及实例的使用介绍
2013/04/24 Javascript
JS绘制生成花瓣效果的方法
2015/08/05 Javascript
JavaScript保留关键字汇总
2015/12/01 Javascript
详解Angularjs中的依赖注入
2016/03/11 Javascript
Bootstrap框架的学习教程详解(二)
2016/10/18 Javascript
Javascript highcharts 饼图显示数量和百分比实例代码
2016/12/06 Javascript
微信小程序 二维码canvas绘制实例详解
2017/01/06 Javascript
利用Jquery实现几款漂亮实用的时间轴(附示例代码)
2017/02/15 Javascript
jQuery插件FusionCharts绘制的3D环饼图效果示例【附demo源码】
2017/04/02 jQuery
Node.js中Bootstrap-table的两种分页的实现方法
2017/09/18 Javascript
python使用webbrowser浏览指定url的方法
2015/04/04 Python
详解Python的Django框架中Manager方法的使用
2015/07/21 Python
Python使用pyautocad+openpyxl处理cad文件示例
2019/07/11 Python
在python中实现同行输入/接收多个数据的示例
2019/07/20 Python
python实现高斯投影正反算方式
2020/01/17 Python
python如何提升爬虫效率
2020/09/27 Python
python3处理word文档实例分析
2020/12/01 Python
html5用video标签流式加载的实现
2020/05/20 HTML / CSS
英超联赛的首选足球:Mitre足球
2019/05/06 全球购物
化学实验员岗位职责
2013/12/28 职场文书
医药销售求职信范文
2014/02/01 职场文书
先进集体获奖感言
2014/02/13 职场文书
喷漆工的岗位职责
2014/03/17 职场文书
新春文艺演出主持词
2014/03/27 职场文书
减负增效提质方案
2014/05/23 职场文书
公务员群众路线专题民主生活会发言材料
2014/09/17 职场文书
老人节标语大全
2014/10/08 职场文书
教师批评与自我批评总结
2014/10/16 职场文书
英文投诉信格式
2015/07/03 职场文书
Canvas跟随鼠标炫彩小球的实现
2021/04/11 Javascript
如何有效防止sql注入的方法
2021/05/25 SQL Server
选购到合适的激光打印机
2022/04/21 数码科技