最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php正則替換處理HTML頁面的方法

 更新時間:2015年06月17日 16:18:59   作者:邪惡的小Y  
這篇文章主要介紹了php正則替換處理HTML頁面的方法,涉及php針對html頁面常見元素的匹配技巧,需要的朋友可以參考下

本文實例講述了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內(nèi)容
  * @param string $relative 相對路徑
  * @param int $projectid 項目id
  * @moudle string $moudle 模板標(biāo)識: 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 */

PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:

JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript

正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

  • php下Memcached入門實例解析

    php下Memcached入門實例解析

    這篇文章主要介紹了php下Memcached入門實例,較為詳細(xì)的分析了memcached的概念與用法,是非常實用的技巧,需要的朋友可以參考下
    2015-01-01
  • 通用PHP動態(tài)生成靜態(tài)HTML網(wǎng)頁的代碼

    通用PHP動態(tài)生成靜態(tài)HTML網(wǎng)頁的代碼

    最近研究PHP的一些開發(fā)技術(shù),發(fā)現(xiàn)PHP有很多ASP所沒有的優(yōu)秀功能,可以完成一些以前無法完成的功能,例如動態(tài)生成HTML靜態(tài)頁面,以減少服務(wù)器CPU的負(fù)載,提高用戶訪問的速度。
    2010-03-03
  • Centos 6.5下PHP 5.3安裝ffmpeg擴展的步驟詳解

    Centos 6.5下PHP 5.3安裝ffmpeg擴展的步驟詳解

    大家都知道ffmpeg是一款視頻流的軟件了,我們在linux系統(tǒng)中可以安裝ffmpeg了,這篇文章主要介紹了在Centos 6.5下PHP 5.3安裝ffmpeg擴展的步驟,需要的朋友可以參考下。
    2017-03-03
  • PHP設(shè)計模式之結(jié)構(gòu)模式的深入解析

    PHP設(shè)計模式之結(jié)構(gòu)模式的深入解析

    本篇文章是對PHP設(shè)計模式中的結(jié)構(gòu)模式進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 使用PHPWord生成word文檔的方法詳解

    使用PHPWord生成word文檔的方法詳解

    這篇文章主要介紹了使用PHPWord生成word文檔的方法,結(jié)合實例形式詳細(xì)分析了PHPWord生成word文檔的具體操作步驟與相關(guān)使用技巧,需要的朋友可以參考下
    2019-06-06
  • 解析PHP獲取當(dāng)前網(wǎng)址及域名的實現(xiàn)代碼

    解析PHP獲取當(dāng)前網(wǎng)址及域名的實現(xiàn)代碼

    本篇文章是對PHP獲取當(dāng)前網(wǎng)址及域名的代碼進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP的SQL注入過程分析

    PHP的SQL注入過程分析

    今天從網(wǎng)上學(xué)習(xí)了有關(guān)SQL注入的基本技能,SQL注入的重點就是構(gòu)造SQL語句,只有靈活的運用SQL語句才能構(gòu)造出牛的注入字符串
    2012-01-01
  • PHP實現(xiàn)的DES加密解密類定義與用法示例

    PHP實現(xiàn)的DES加密解密類定義與用法示例

    這篇文章主要介紹了PHP實現(xiàn)的DES加密解密類定義與用法,結(jié)合實例形式分析了基于php定義的DES加密解密類與具體使用方法,需要的朋友可以參考下
    2018-07-07
  • PHP中危險的file_put_contents函數(shù)詳解

    PHP中危險的file_put_contents函數(shù)詳解

    file_put_contents() 函數(shù)把一個字符串寫入文件中。最近發(fā)現(xiàn)file_put_contents函數(shù)有一直沒注意到的問題,所以下面這篇文章主要給大家介紹了關(guān)于PHP中危險的file_put_contents函數(shù)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看吧。
    2017-11-11
  • php讀取mysql亂碼,用set names XXX解決的原理分享

    php讀取mysql亂碼,用set names XXX解決的原理分享

    解決亂碼的方法,我們經(jīng)常使用“set names utf8”,那么為什么加上這句代碼就可以解決了呢?下面跟著我一起來深入set names utf8的內(nèi)部執(zhí)行原理
    2011-12-12

最新評論

扎赉特旗| 颍上县| 密山市| 凤冈县| 宁河县| 黑龙江省| 云霄县| 光山县| 遂溪县| 自治县| 泊头市| 靖边县| 银川市| 定兴县| 丹东市| 丰台区| 三原县| 威信县| 吉安县| 响水县| 阿勒泰市| 凤山市| 南丹县| 界首市| 汉阴县| 汉寿县| 交口县| 双桥区| 垣曲县| 增城市| 固阳县| 雷州市| 莫力| 梅州市| 鄂伦春自治旗| 沂水县| 大荔县| 孝义市| 上犹县| 县级市| 宁陕县|