php正则替换处理HTML页面的方法


Posted in PHP onJune 17, 2015

本文实例讲述了php正则替换处理HTML页面的方法。分享给大家供大家参考。具体如下:

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
 /**
 * HTML替换处理类,考虑如下几种替换
 * 1. img src : '/<img(.+?)src=([\'\" ])?(.+?)([ >]+?)/i'
 * 2. a href : '/<a(.+?)href=([\'\" ])?(.+?)([ >]+?)/i'
 * 3. ifram.src : '/<iframe(.+?)src=([\'\" ])?(.+?)([ >]+?)/i'
 * 4. frame src : '/<frame(.+?)src=([\'\" ])?(.+?)([ >]+?)/i'
 * 5. js : '/window.open([( ]+?)([\'" ]+?)(.+?)([ )+?])/i'
 * 6. css : '/background(.+?)url([( ])([\'" ]+?)(.+?)([ )+?])/i'
 */
 class Myreplace {
 private $moudle_array = array('udata','tdata','tresult','dresult');
 private $content;
 private $relative_dirname;
 private $projectid;
 private $moudle;
 function __construct() {
  $this->CI = &get_instance ();
 }
 /**
  * 替换
  * @param string $content HTML内容
  * @param string $relative 相对路径
  * @param int $projectid 项目id
  * @moudle string $moudle 模板标识: udata,tdata,tresult,dresult
  */
 public function my_replace($content,$relative,$projectid,$moudle) {
  $this->content = $content;
  $this->relative_dirname = $relative;
  $this->projectid = $projectid;
  if(in_array(strtolower($moudle),$this->moudle_array))
  $this->moudle = $moudle;
  else exit;
  switch($this->moudle) {
  case 'udata':
   $this->CI->load->model('mupload_data','model');
   break;
  case 'tdata':
   $this->CI->load->model('taskdata','model');
   break;
  case 'tresult':
   $this->CI->load->model('taskresult','model');
   break;
  case 'dresult':
   $this->CI->load->model('dmsresult','model');
   break;
  default:
   break;
  }
  $pattern = '/<img(.+?)src=([\'\" ])?(.+?)([ >]+?)/i';
  $content = preg_replace_callback( $pattern, array($this, 'image_replace') , $content );
  $pattern = '/<a(.+?)href=([\'\" ])?(.+?)([ >]+?)/i';
  $content = preg_replace_callback( $pattern, array($this, 'html_replace') , $content );
  $pattern = '/<iframe(.+?)src=([\'\" ])?(.+?)([ >]+?)/i';
  $content = preg_replace_callback( $pattern, array($this, 'iframe_replace') , $content );
  $pattern = '/<frame(.+?)src=([\'\" ])?(.+?)([ >]+?)/i'; 
  $content = preg_replace_callback( $pattern, array($this, 'frame_replace'), $content );
  $pattern = '/window.open([( ]+?)([\'" ]+?)(.+?)([ )]+?)/i';
  $content = preg_replace_callback( $pattern, array($this, 'js_replace'), $content );
  $pattern = '/background(.+?)url([( ])([\'" ]+?)(.+?)([ )+?])/i';
  $content = preg_replace_callback( $pattern, array($this, 'css_replace'), $content);
  return $content;
 }
 private function image_replace($matches) {
  if(count($matches) < 4) return '';
  if( empty($matches[3]) ) return '';
  $matches[3] = rtrim($matches[3],'\'"/');
  //获取图片的id
  $parent_dir_num = substr_count( $matches[3], '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');
  $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($image_id) ) {
  if($this->moudle == 'dresult') {
   return "<img".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readpic/$image_id?pid=".$this->projectid .$matches[2]. $matches[4];
  } else {
   return "<img".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/picfile/$image_id?pid=".$this->projectid .$matches[2]. $matches[4];
  }
  } else {
  return "<img".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4];
  }
 }
 private function html_replace( $matches ) {
  if(count($matches) < 4) return '';
  if( empty($matches[3]) ) return '';
  //如果href的链接($matches[3])以http或www或mailto开始,则不进行处理
  //if(preg_match('/^[http|www|mailto](.+?)/i',$matches[3])) 
  // return "<a".$matches[1]."href=".$matches[2].$matches[3].$matches[4];
  $matches[3] = rtrim($matches[3],'\'"/');
  //处理锚点
  if(substr_count($matches[3],'#')>0) 
  $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  //获取html的id
  $parent_dir_num = substr_count( $matches[3], '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');
  $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($txtfile_id ) ) {
  if($this->moudle == 'dresult') {
   return "<a".$matches[1]."href=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];
  } else {
   return "<a".$matches[1]."href=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];
  }
  } else {
  return "<a".$matches[1]."href=".$matches[2].$matches[3].$matches[2].$matches[4];
  }
 }
 private function iframe_replace( $matches ) {
  if(count($matches) < 4) return '';
  if( empty($matches[3]) ) return '';
  $matches[3] = rtrim($matches[3],'\'"/');
  //处理锚点
  if(substr_count($matches[3],'#')>0) 
  $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  //获取html的id
  $parent_dir_num = substr_count( $matches[3], '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');
  $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($txtfile_id ) ) {
  if($this->moudle == 'dresult') { 
   return "<iframe".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];
  } else {
   return "<iframe".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];
  }
  } else {
  return "<iframe".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4];
  }
 }
 private function frame_replace( $matches ) {  
  if(count($matches) < 4) return '';
  if( empty($matches[3]) ) return '';
  $matches[3] = rtrim($matches[3],'\'"/');
  //处理锚点
  if(substr_count($matches[3],'#')>0) 
  $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));
  //获取html的id
  $parent_dir_num = substr_count( $matches[3], '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');
  $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($txtfile_id ) ) {
  if($this->moudle == 'dresult') { 
   return "<frame".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];
  } else {
   return "<frame".$matches[1]."src=".$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];
  }
  } else {
  return "<frame".$matches[1]."src=".$matches[2].$matches[3].$matches[2].$matches[4];
  }
 }
 private function js_replace( $matches ){
  if(count($matches) < 4) return '';
  if( empty($matches[3]) ) return '';
  //处理链接
  $arr_html = split(',',$matches[3]);
  $href = $arr_html[0];
  $other = '';
  for($i=0; $i<count($arr_html); $i++)
  $other = $arr_html[$i].", ";
  $other = rtrim($other,"\, ");
  $href =rtrim($href,'\'\"');
  //处理锚点
  if(substr_count($href,'#')>0) 
  return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];;
  //获取html的id
  $parent_dir_num = substr_count( $href, '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($href,'./');
  $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($txtfile_id ) ) {
  if($this->moudle == 'dresult') { 
   return "window.open".$matches[1].$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].','.$other.$matches[4];
  } else {
   return "window.open".$matches[1].$matches[2].$this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].','.$other.$matches[4];
  }
  } else {
  return "window.open".$matches[1].$matches[2].$matches[3].$matches[4];
  }
 }
 private function css_replace( $matches ) {
  if(count($matches) < 5) return '';
  if( empty($matches[4]) ) return '';
  
  $matches[4] = rtrim($matches[4],'\'"/');
  //获取图片的id
  $parent_dir_num = substr_count( $matches[4], '../');
  $relative_dirname = $this->relative_dirname;
  for($i=0; $i<$parent_dir_num; $i++) {
  $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );
  }
  $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[4],'./');
  $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);
  //输出
  if( !empty($image_id) ) {
  if($this->moudle == 'dresult') {
   return "background".$matches[1]."url".$matches[2].$matches[3].$this->CI->config->item("base_url")."cdms/".$this->moudle."/readpic/$image_id?pid=".$this->projectid .$matches[3]. $matches[5];
  } else {
   return "background".$matches[1]."url".$matches[2].$matches[3].$this->CI->config->item("base_url")."cdms/".$this->moudle."/picfile/$image_id?pid=".$this->projectid .$matches[3]. $matches[5];
  }
  } else {
  return "background".$matches[1]."url".$matches[2].$matches[3].$matches[4].$matches[3].$matches[5];
  }
 }
 }
/* End of Myreplace.php */
/* Location: /application/libraries/Myreplace.php */
PHP 相关文章推荐
约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数
Oct 12 PHP
发款php蜘蛛统计插件只要有mysql就可用
Oct 12 PHP
一些需要禁用的PHP危险函数(disable_functions)
Feb 23 PHP
表格展示无限级分类(PHP版)
Aug 21 PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
Jan 26 PHP
php简单的上传类分享
May 15 PHP
php rmdir使用递归函数删除非空目录实例详解
Oct 20 PHP
LNMP部署laravel以及xhprof安装使用教程
Sep 14 PHP
Laravel框架路由设置与使用示例
Jun 12 PHP
PHP htmlspecialchars()函数用法与实例讲解
Mar 08 PHP
PHP7内核之Reference详解
Mar 14 PHP
PHP设计模式(六)桥连模式Bridge实例详解【结构型】
May 02 PHP
PHP排序算法类实例
Jun 17 #PHP
php准确获取文件MIME类型的方法
Jun 17 #PHP
php操作MongoDB类实例
Jun 17 #PHP
PHP实现的购物车类实例
Jun 17 #PHP
CodeIgniter实现从网站抓取图片并自动下载到文件夹里的方法
Jun 17 #PHP
PHP基于MySQL数据库实现对象持久层的方法
Jun 17 #PHP
php使用curl打开https网站的方法
Jun 17 #PHP
You might like
劣质的PHP代码简化
2010/02/08 PHP
PHP程序员基本要求和必备技能
2014/05/09 PHP
Laravel 自定命令以及生成文件的例子
2019/10/23 PHP
使用insertAfter()方法在现有元素后添加一个新元素
2014/05/28 Javascript
Javascript中setTimeOut和setInterval的定时器用法
2015/06/12 Javascript
在Ubuntu系统上安装Ghost博客平台的教程
2015/06/17 Javascript
JavaScript设计模式之单体模式全面解析
2016/09/09 Javascript
ES6正则表达式的一些新功能总结
2017/05/09 Javascript
Vue.js学习笔记之修饰符详解
2017/07/25 Javascript
jQuery基于Ajax实现读取XML数据功能示例
2018/05/31 jQuery
详解angular应用容器化部署
2018/08/14 Javascript
ES6基础之解构赋值(destructuring assignment)
2019/02/21 Javascript
js实现车辆管理系统
2020/08/26 Javascript
如何在JavaScript中等分数组的实现
2020/12/13 Javascript
介绍Python中的fabs()方法的使用
2015/05/14 Python
python中pandas.DataFrame对行与列求和及添加新行与列示例
2017/03/12 Python
python中正则表达式的使用方法
2018/02/25 Python
Python爬虫使用脚本登录Github并查看信息
2018/07/16 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
2018/12/22 Python
Python实现的大数据分析操作系统日志功能示例
2019/02/11 Python
Jacobi迭代算法的Python实现详解
2019/06/29 Python
win10下python2和python3共存问题解决方法
2019/12/23 Python
Python中zip函数如何使用
2020/06/04 Python
Python Pillow(PIL)库的用法详解
2020/09/19 Python
基于HTML5代码实现折叠菜单附源码下载
2015/11/27 HTML / CSS
机械工程师求职自我评价
2013/09/23 职场文书
应届生船舶驾驶求职信
2013/10/19 职场文书
销售员岗位职责范本
2014/02/03 职场文书
股东授权委托书
2014/10/15 职场文书
群众路线四风对照检查材料
2014/11/04 职场文书
售后服务承诺函格式
2015/01/21 职场文书
民主评议党员个人总结
2015/02/13 职场文书
仓库统计员岗位职责
2015/04/14 职场文书
教师节随笔
2015/08/15 职场文书
在redisCluster中模糊获取key方式
2021/07/09 Redis
速龙x4-860k处理器相当于i几
2022/04/20 数码科技