php 实现类,php 获取页面中指定内容的实现类
本文为大家下使用php如何获取页面中的指定内容,而且以封装成类,需要的朋友可以参考下本文
功能:
1.获取内容中的url,email,image。
2.替换内容中的url,email,image。
url:xxx
email:admin@admin.com
image:
Grep.class.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' => '/
'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/',
'image' => '//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
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 获取页面中指定内容的实现类相关推荐
- php获取页面中的指定内容,php 获取页面中指定内容的实现类
[email protected] image: Grep.class.php /** grep class * Date: 2013-06-15 * Author: fdipzone * Ver: ...
- js html保存word文档,js将页面中指定内容保存到WORD
在网页WEB中,我们可以借助JS代码,将页面中指定的内容或特定的局部内容,而不是整个页面,导出到WORD文档,进而可以通过WORD保存的方法,生成WORD文件. 通过这种方法,在导出的时候,如果是第一 ...
- 当前元素_前端系列——获取页面中的DOM元素
这里就聊一下获取页面中DOM元素最基本的两种方法: document.getElementById. 在整个页面中通过元素的Id属性值来获取到这个元素对象,getElementById是获取元素的方法 ...
- python提取矩阵元素_python获取array中指定元素的示例
python获取array中指定元素的示例 对于array,如2-D的array,如何取指定元素 设array为3*10的shape s = array([[ 0, 1, 2, 3, 4, 5, 6, ...
- Python 获取list 中指定元素出现的次数
Python 获取list 中指定元素出现的次数 使用list 的count 方法 list_a = [1, 2, 3, 1, 4, 5, 1] print("1 出现的次数: %d&quo ...
- java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表
集合类中的 List.subList() 方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法: subList(int fromIndex,i ...
- 如何获取FragmentTabHost中指定标签页的Fragment
使用FragmentTabHost构建了包含几个标签页的界面,如何获取指定标签页的Fragment? How to get Fragment in FragmentTabHost? TabHost中F ...
- php 取出多重数组中的一列_PHP获取数组中指定的一列实例
对于一个多维数组(以二维数组为例),经常需要获取到其中的一列,比如有几条用户数据,是一个二维数组,现在需要获取到这几个用户的名字,可以有多种方法实现: $arr = array( array( 'id ...
- [js] 写一个获取页面中所有checkbox的方法
[js] 写一个获取页面中所有checkbox的方法 function getAllCheckbox() {return [...document.querySelectorAll('input[ty ...
最新文章
- ALAsset 循环获取图片(Assets Library Framework)
- python中for语句的使用_对Python中for复合语句的使用示例讲解
- Java面试宝典2018
- 计算机信息安全专业代码0839,网络安全/信息安全专业大学排名(2017-2018-安全导航)...
- 中文汉字转拼音首字母大写
- mysql二级考试范围_计算机二级MySQL考试内容大纲
- gcc -wall -pedantic -ansi
- Uncaught Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered d
- obj转stl_STL转STP的方法视频教程,OBJ格式转STP或者IGS开模具格式的过程,STL转STP软件介绍...
- Mac电脑下载的google chrome无法使用
- PWC-Net: CNNs for Optical Flow Using Pyramid, Warping, and Cost Volume
- h5发送短信以及判别用户浏览器版本
- java中的Dao类是什么意思
- js实现3D旋转相册
- Vue入门(个人理解)
- 安卓系统导航测试软件,安卓战GPS 凯立德导航软件实测解析
- 1.人工智能早期知识表示方法
- linux程序包覆盖安装,Linux—程序包安装与管理
- VLC播放器应用------LIBVLC API解析
- 请勿在计算机室吃带果壳的食品英语,吃了这么多包装食品,还不懂营养标签?...
热门文章
- 微软Azure予力Green Dot客服成就不凡
- 11月7日邀您参加成都微软MVP圆桌之夜!
- ASP.NET Core - Razor 页面介绍
- ArcGIS实验教程——实验三十七:基于ArcGIS的太阳辐射分析案例教程
- 完美解决ArcGIS10.2和Erdas9.2软件冲突的方法:共存!
- Android之运行PopupWindow提示Unable to add window -- token null is not valid; is your activity running?
- java之异常java.net.MalformedURLException解决办法
- C++之operator关键字(重载操作符) 使用总结
- python数据类型描述_【文山玩Python】用python的数据类型,来简单的描述世界
- android auto answer,Incoming call auto answer in android 4.0.3