<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(!defined('BASEPATH')) exit('No direct script access allowed');<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;</li> <li> private $projectid;</li> <li> private $moudle;</li> <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 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> break;</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> break;</li> <li> default:</li> <li> break;</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> $content = 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 = '/background(.+?)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> }</li> <li> $relativepath = rtrim($relative_dirname,'/') . '/'.ltrim($matches[3],'./');</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 "<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> //如果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,'/') . '/'.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 "<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($matches[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 = rtrim($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 "<iframe>CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid .$matches[2].$matches[4];<li> } else {</li> <li> return "<iframe>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> $relativepath = rtrim($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>CI->config->item("base_url")."cdms/".$this->moudle."/readfile/$txtfile_id?pid=".$this->projectid.$matches[2].$matches[4];</li> <li> } 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) 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($i=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> return "background".$matches[1]."url".$matches[2].$matches[3].$matches[4].$matches[3].$matches[5];</li> <li> }</li> <li> }</li> <li> }</li> <li> <li>/* End of Myreplace.php */</li> <li>/* Location: /application/libraries/Myreplace.php */</li></count> </li></iframe> </li></iframe> </li></iframe> </li></a> </li></a> </li></a> </li></a> </li></iframe> </li></a> </li></iframe> </li></a> </li> </ol></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>