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

2015-01-24信息快讯网

本文为大家下使用php如何获取页面中的指定内容,而且以封装成类,需要的朋友可以参考下本文

功能:

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 = ''; // 源内容 


/* 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){ 

$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大批量插入数据库的3种方法和速度对比
php发送get、post请求的6种方法简明总结
PHP中对各种加密算法、Hash算法的速度测试对比代码
最常用的8款PHP调试工具
一个不易被发现的PHP后门代码解析
PHPMailer发送HTML内容、带附件的邮件实例
PHP中使用CURL模拟登录并获取数据实例
PHP获取时间排除周六、周日的两个方法
jQuery+PHP+ajax实现微博加载更多内容列表功能
ThinkPHP采用GET方式获取中文参数查询无结果的解决方法
实现在同一方法中获取当前方法中新赋值的session值解决方法
PHP获取windows登录用户名的方法
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
ThinkPHP3.1新特性之内容解析输出详解
php获取网页中图片、DIV内容的简单方法
PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)
PHP获取指定函数定义在哪个文件中以及其所在的行号实例
PHP正则提取不包含指定网址的图片地址的例子
php生成随机字符串可指定纯数字、纯字母或者混合的
PHP判断指定时间段的2个方法
php 发送带附件邮件示例
php 根据url自动生成缩略图并处理高并发问题
php 字符串压缩方法比较示例
php 生成短网址原理及代码
解决php接收shell返回的结果中文乱码问题
php弹出对话框实现重定向代码
php利用新浪接口查询ip获取地理位置示例
php利用腾讯ip分享计划获取地理位置示例分享
php生成txt文件标题及内容的方法
PHP获取php,mysql,apche的版本信息示例代码
php获取淘宝分类id示例
PHP curl 获取响应的状态码的方法
php获取文件内容最后一行示例
php上传图片到指定位置路径保存到数据库的具体实现
php函数指定默认值方法的小例子
php指定函数参数默认值示例代码
php对二维数组按指定键值key排序示例代码
php使用mb_check_encoding检查字符串在指定的编码里是否有效
腾讯QQ微博API接口获取微博内容
使用Curl进行抓取远程内容时url中文编码问题示例探讨
php一次性删除前台checkbox多选内容的方法
©2014-2024 dbsqp.com