php正則替換處理HTML頁面的方法
本文實(shí)例講述了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 相對(duì)路徑 * @param int $projectid 項(xiàng)目id * @moudle string $moudle 模板標(biāo)識(shí): 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開始,則不進(jìn)行處理 //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],'\'"/'); //處理錨點(diǎn) 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],'\'"/'); //處理錨點(diǎn) 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],'\'"/'); //處理錨點(diǎn) 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,'\'\"'); //處理錨點(diǎn) 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á)式在線測(cè)試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP preg_replace() 正則替換所有符合條件的字符串
- PHP 字符串正則替換函數(shù)preg_replace使用說明
- PHP正則替換函數(shù)preg_replace和preg_replace_callback使用總結(jié)
- 如何在PHP中使用正則表達(dá)式進(jìn)行查找替換
- PHP html標(biāo)簽正則替換并可自定義正則規(guī)則
- PHP把空格、換行符、中文逗號(hào)等替換成英文逗號(hào)的正則表達(dá)式
- php正則替換變量指定字符的方法
- php正則取img標(biāo)記中任意屬性(正則替換去掉或改變圖片img標(biāo)記中的任意屬性)
- php中正則替換函數(shù)ereg_replace用法實(shí)例
- 詳解PHP正則表達(dá)式替換實(shí)現(xiàn)(PHP preg_replace,PHP preg_replace)
- php中preg_replace正則替換用法分析【一次替換多個(gè)值】
- PHP實(shí)現(xiàn)通過正則表達(dá)式替換回調(diào)的內(nèi)容標(biāo)簽
- PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能示例
相關(guān)文章
PHP PDO數(shù)據(jù)庫操作預(yù)處理與注意事項(xiàng)
今天小編就為大家分享一篇關(guān)于PHP PDO數(shù)據(jù)庫操作預(yù)處理與注意事項(xiàng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Thinkphp結(jié)合ajaxFileUpload實(shí)現(xiàn)異步圖片傳輸示例
這篇文章主要給大家介紹了利用Thinkphp結(jié)合ajaxFileUpload實(shí)現(xiàn)異步圖片傳輸?shù)姆椒?,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03原生php實(shí)現(xiàn)excel文件讀寫的方法分析
這篇文章主要介紹了原生php實(shí)現(xiàn)excel文件讀寫的方法,結(jié)合實(shí)例形式分析了采用原生php針對(duì)Excel進(jìn)行讀寫操作的相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2018-04-04Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解
這篇文章主要介紹了Laravel5.5+ 使用API Resources快速輸出自定義JSON方法詳解,需要的朋友可以參考下2020-04-04php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法
這篇文章主要介紹了php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法,分享了4種PHP異步執(zhí)行的常用方式,感興趣的小伙伴們可以參考一下2015-12-12WampServer搭建php環(huán)境時(shí)遇到的問題匯總
這篇文章主要介紹了WampServer搭建php環(huán)境時(shí)遇到的常見問題的解決辦法匯總,以上所述就是本文的全部內(nèi)容了。2015-07-07php判斷輸入不超過mysql的varchar字段的長度范圍
varchar類型字段,如果你設(shè)置長度為10,那么不論漢字和英文都可以存10個(gè)。2011-06-06PHP中使用DOMDocument來處理HTML、XML文檔的示例
這篇文章主要介紹了PHP中使用DOMDocument來處理HTML、XML文檔的示例,幫助大家更好的理解和學(xué)習(xí)使用php語言,感興趣的朋友可以了解下2021-04-04利用Ffmpeg獲得flv視頻縮略圖和視頻時(shí)間的代碼
谷歌了半天發(fā)現(xiàn)可以使用Ffmpeg獲得視頻的一些信息,先介紹一下FFMEPG2011-09-09