<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_19261"> HTML預覽 正規替換 <br><div class="blockcode"> <div id="code_PgI"> <ol> <li> <?php if(!define('BASEPATH')) exit('不允許直接腳本存取');<li> /**</li> <li> * HTML替換處理類,考慮以下幾種替換</li> <li> * 1. img src : '/<img alt="HTML預覽 正規替換" >] +?)/i'</li> <li> * 2. a href : '/<a>]+?)/i'<li> * 3. ifram.src : '/<iframe>]+?)/i'<li> * 4. frame src : '/<frame>]+?)/i'</li> <li> * 5. js : '/window.open([( ] +?)(['" ]+?)(.+?)([ )+?])/i'</li> <li> * 6. css : '/background(.+?)url([( ])([ '" ]+?)(.+?)([ )+?])/i'</li> <li>*/</li> <li> class Myreplace {</li> <li> private $moudle_array = array('udata','tdata','tresult','dresult');</li> <li> private $content;</li> <li> private $relative_dirname;<profate> private $moudle;<li> function __construct() {</li><li> $this->CI = &get_instance ();</li><li> }</li><li> </li><li> /**</li><li> * 替換</li><li> * @param string $content HTML內容</li><li> * @param string $relative 相對路徑</li><li> * @param int $projectid 專案id</li><li> * @moudle string $moudle 範本識別udata,tdata,tresult,dresult</li><li>*/</li><li> public</li><li> public</li><li> function my_replace($content,$relative,$projectid,$moudle) {</li><li> $this->content = $content;</li><li> $this->relative_dirname = $relative;</li><li> $this->projectid = $ projectid;</li><li> if(in_array(strtolower($moudle),$this->moudle_array))</li><li> $this->moudle = $moudle;</li><li> else exit;</li><li> switch($this->moudle ) {</li><li> case 'udata':</li><li> $this->CI->load->model('mupload_data','model');</li><li> break; </li><li> case 'tdata':</li><li> $this->CI->load->model('taskdata','model');</li><li> 中斷;</li><li> case 'tresult':</li><li> $this ->CI->load->model('taskresult','model');</li><li> break;</li><li> case 'dresult':</li><li> $this->CI->; load->model('dmsresult','model');</li><li> 規則;</li><li> 預設值:</li><li> 規則;</li><li> }</li><li> </li><li> $pattern = '/<img alt="HTML預覽 正規替換" >]+?)/i';</li><li> $content = preg_replace_callback( $pattern, array($this, 'image_replace') , $content );</li><li> $pattern = '/<a>]+?)/i';<li> $內容= preg_replace_callback( $pattern, array($this, 'html_replace') , $content );</li> <li> $pattern = '/<iframe>] +?)/i';<li> $content = preg_replace_callback( $pattern, array($this, 'iframe_replace') , $content );</li> <li> $pattern = '/<frame .>]+?)/i'; </li> <li> $content = preg_replace_callback( $pattern, array($this, 'frame_replace'), $content );</li> <li> $pattern = '/window.open([( ]+?)(['" ]+? )(.+?)([ )]+?)/i';</li> <li> $content = preg_replace_callback( $pattern, array($this, 'js_replace'), $content );</li> <li> $pattern = '/背景(.+?)url([( ])(['" ]+?)(.+?)([ )+?])/i';</li> <li> $content = preg_replace_callback( $pattern, array($ this, 'css_replace'), $content);</li> <li> return $content;</li> <li> }</li> <li> </li> <li> private function image_replace($matches) {</li> <li> if(count($matches) if(empty($matches[3]) ) return '';</li> <li> $matches[3] = rtrim($matches[3],''"/');</li> <li> //取得圖片的id</li> <li> $parent_dir_num = substr_count( $matches[3], '../');</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($i=0; $ i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );</li> <li> }<path> $relative = rtrim($relative_irname) '.ltrim($matches[3],'./');<li> $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this-> projectid);</li><li> // 輸出</li><li> if( !empty($image_id) ) {</li><li> if($this->moudle == 'dresult') {</li><li> 回傳「<img alt="HTML預覽 正規替換" >CI->config->item("base_url")."cdms/".$this->moudle." /readpic/$ image_id?pid=".$this->projectid .$matches[2]. $matches[4];</li><li> } else {</li><li> return "<img alt="HTML預覽 正規替換" >CI->config->item( "base_url")."cdms/".$this->moudle."/picfile/$image_id?pid=".$this->projectid .$matches[2]. $matches[4];</li><li> }</li><li> } else {</li><li> return "<img alt="HTML預覽 正規替換" > }</li><li> }</li><li> </li><li> private function html_replace( $matches ) {</li><li> if(count($matches) if( empty($matches[3]) ) return '';</li><li> if( empty($matches[3]) ) return '';</li><li> //如果href的連結($matches[3])以http或www或mailto開始,則不進行處理</li><li> //if(preg_match('/^[http|www|mailto](.+? )/i',$matches[3])) </li><li> // return "<a> $matches[3] = rtrim($matches[3],''"/');<li> //處理錨點</li> <li> if(substr_count($matches[3],'#' )>0) </li> <li> $matches[3] = substr($matches[3],0,strrpos($matches[3],'#')); </li> <li> //取得html的id</li> <li> $ parent_dir_num = substr_count( $matches[3], '../');</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($i=0; $i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );</li> <li> }</li> <li> $relativepath = rtrim($relative_dirname,'/') . '/$ matches[3],'./');</li> <li> $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);</li> <li> //輸出</li> <li> if( !empty($txtfile_id ) ) {</li> <li> if($this->moudle == 'dresult') {</li> <li> return "<a .>CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2 ].$matches[4];<li> } else {</li> <li> return "<a>CI->config-> item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];<li> }</li> <li> } else {</li> <li> return "<a> }<li> }</li> <li> </li> <li> private function iframe_replace( $matches ) {</li> <li> if(count($matches) if( empty($ches[ 3]) ) return '';</li> <li> $matches[3] = rtrim($matches[3],''"/');</li> <li> </li> <li> //處理錨點</li> <li> if(substr_count( $matches[3],'#')>0) </li> <li> $matches[3] = substr($matches[3],0,strrpos($matches[3],'#'));</li> <li> / /取得html的id</li> <li> $parent_dir_num = substr_count( $matches[3], '../');</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($i=0; $i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );</li> <li> }</li> <li> $relativepath = xrim($relative_irinname' ) . '/'.ltrim($matches[3],'./');</li> <li> $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);</li> <li> //輸出</li> <li> if( !empty($txtfile_id ) ) {</li> <li> if($this->moudle == 'dresult') { </li> <li> return "<iframe src=".%24matches%5B2%5D.%24this->CI->config->item(" base_url>moudle."/readfile/$txtfile_id?pid=".$ this->projectid .$matches[2].$matches[4];<li> } else {</li> <li> return "<iframe this->CI->config->item("base_url")."cdms/".$this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid .$matches[2]. $matches[4];<li> }</li> <li> } else {</li> <li> return "<iframe> }<li> }</li> <li> </li> <li> private function frame_replace( $matches ) { </li> <li> if(count($matches) if( empty($matches[3]) ) return '';</li> <li> $matches[3] = rtrim($matches[3],''"/');</li> <li> //處理錨點</li> <li> if(substr_count($matches[3],'#')>0) </li> <li> $matches[3] = substr($matches[3],0,strrpos($matches[3],'#') );</li> <li> //取得html的id</li> <li> $parent_dir_num = substr_count( $matches[3], '../');</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($ i=0; $i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );</li> <li> }</li> <li> $relative = rimrim( $relative_dirname,'/') . '/'.ltrim($matches[3],'./');</li> <li> $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this- >projectid);</li> <li> //輸出</li> <li> if( !empty($txtfile_id ) ) {</li> <li> if($this->moudle == 'dresult') { </li> <li> return "<frame matches>CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id ?pid=".$this->projectid.$matches[2].$matches[4]; }else {</li> <li> return "<frame>CI->config->item("base_url")." cdms/". $this->moudle."/txtfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];</li> <li> }</li> <li> } else {</li> <li> return " <frame> } </li> <li> }</li> <li> </li> <li> private function js_replace( $matches ){</li> <li> if(count($matches) if(empty($matches[3]) ) return '';</li> <li> / /處理連結</li> <li> $arr_html = split(',',$matches[3]);</li> <li> $href = $arr_html[0];</li> <li> $other = '' ;</li> <li> for($i =0; $i<count> $other = $arr_html[$i].", ";<li> $other = rtrim($other ,", ");</li> <li> $href =rtrim($href,''"');</li> <li> //處理節點</li> <li> if(substr_count($href,'#')>0) </li> <li> return "window.open".$ matches[1 ].$matches[2].$matches[3].$matches[4];;</li> <li> //取得html的id</li> <li> $parent_dir_num = substr_count ( $href, '../') ;</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($i=0; $i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname) /") );</li> <li> }</li> <li> $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($href,'./');</li> <li> $txtfile_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid);</li> <li> // 輸出</li> <li> if( !empty($txtfile_id ) ) {</li> <li> if($this->moudle == 'dresult') { </li> <li> 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];</li> <li> } else {</li> <li> 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];</li> <li> }</li> <li> } else {</li> <li> return "window.open".$matches[1].$matches [2].$matches[3] .$matches[4];</li> <li> }</li> <li> }</li> <li> </li> <li> private function css_replace( $matches ) {</li> <li> if(count($matches) </li> <li> 5) return '';</li> <li> if(empty($matches[4]) ) return '';</li> <li> </li> <li> $matches[4] = rtrim($matches[4],''"/') ;</li> <li> //取得圖片的id</li> <li> $parent_dir_num = substr_count( $matches[4], '../');</li> <li> $relative_dirname = $this->relative_dirname;</li> <li> for($我=0; $i $relative_dirname = substr( $relative_dirname, 0, strrpos($relative_dirname,"/") );</li> <li> }</li> <li> $relativepath = rtrim($relative_dirname, '/') 。 /'.ltrim($matches[4],'./');</li> <li> $image_id = $this->CI->model->get_id_by_path_and_project($relativepath,$this->projectid); </li> <li> // 輸出</li> <li> if( !empty($image_id) ) {</li> <li> if($this->moudle == 'dresult') {</li> <li> 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];</li> <li> } else {</li> <li> 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];</li> <li> }</li> <li> } else {</li> <li> 返回 "背景".$matches[1]."url".$matches[2].$matches[3].$matches[4 ].$matches[3].$matches[5];</li> <li> }</li> <li> }</li> <li> }</li> <li> <li>/* Myreplace.php 結束*/</li></count> </li></iframe> </li></iframe> </li></iframe> </li></a> </li></a> </li></a> </li></a> </li></path> </li></iframe> </li></a> </li></profate> </li></iframe> </li></a> </li> </ol>/* 位置:/application / Librarys/Myreplace.php */</div> <em onclick="copycode($('code_PgI'));"></em>複製程式碼</div> </td></tr></table> <div id="comment_19261" class="cm"> </div> <div id="post_rate_div_19261"> </div> <br> <br>