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 相关文章推荐
php封装好的人民币数值转中文大写类
Dec 20 PHP
Zend Framework教程之Application和Bootstrap用法详解
Mar 10 PHP
再谈PHP中单双引号的区别详解
Jun 12 PHP
php英文单词统计器
Jun 23 PHP
Yii2主题(Theme)用法详解
Jul 23 PHP
PHP简单实现正则匹配省市区的方法
Apr 13 PHP
Laravel框架Request、Response及Session操作示例
May 06 PHP
php依赖注入知识点详解
Sep 23 PHP
laravel 框架结合关联查询 when()用法分析
Nov 22 PHP
php文件上传原理与实现方法详解
Dec 20 PHP
PHPExcel实现的读取多工作表操作示例
Apr 14 PHP
Yii Framework框架开发微信公众平台示例
Apr 26 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新手上路(十三)
2006/10/09 PHP
PHP 文件上传全攻略
2010/04/28 PHP
老生常谈PHP面向对象之注册表模式
2017/05/26 PHP
PHP+Ajax实现的无刷新分页功能详解【附demo源码下载】
2017/07/03 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
php之可变函数的实例详解
2017/09/13 PHP
PHP+Redis开发的书签案例实战详解
2019/07/09 PHP
PHP实现笛卡尔积算法的实例讲解
2019/12/22 PHP
关于JavaScript的with 语句的使用方法
2011/05/09 Javascript
js判断变量初始化的三种形式及推荐用的形式
2014/07/22 Javascript
jQuery fadeOut 异步实例代码详解
2016/08/18 Javascript
JavaScript中ES6字符串扩展方法
2016/08/26 Javascript
JavaScript String(字符串)对象的简单实例(推荐)
2016/08/31 Javascript
获取jqGrid中选择的行的数据
2016/11/30 Javascript
详解VueRouter进阶之导航钩子和路由元信息
2017/09/13 Javascript
浅谈Javascript中的对象和继承
2019/04/19 Javascript
vue实现跨域的方法分析
2019/05/21 Javascript
js生成1到100的随机数最简单的实现方法
2020/02/07 Javascript
javascript 原型与原型链的理解及应用实例分析
2020/02/10 Javascript
python控制台中实现进度条功能
2015/11/10 Python
详解用Python练习画个美队盾牌
2019/03/23 Python
Django时区详解
2019/07/24 Python
使用python实现希尔、计数、基数基础排序的代码
2019/12/25 Python
Pycharm 安装 idea VIM插件的图文教程详解
2020/02/21 Python
Python使用urlretrieve实现直接远程下载图片的示例代码
2020/08/17 Python
python 实现客户端与服务端的通信
2020/12/23 Python
HTML5和CSS3实例教程总结(推荐)
2016/07/18 HTML / CSS
模范家庭事迹材料
2014/02/10 职场文书
《乡愁》教学反思
2014/02/18 职场文书
公司办公室岗位职责
2014/03/19 职场文书
项目经理聘任书
2014/03/29 职场文书
2014年综合治理工作总结
2014/11/20 职场文书
保管员岗位职责
2015/02/14 职场文书
污水处理保证书
2015/05/09 职场文书
房屋转让协议书(标准范本)
2016/03/21 职场文书
python使用pywinauto驱动微信客户端实现公众号爬虫
2021/05/19 Python