1. [代码][PHP]代码  
<?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);
            //输出http://www.bizhizu.cn/shouhui/​
            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 */

HTML预览 正则替换的更多相关文章

  1. webform的原生操作图片预览和上传

    1.使用input标签进行图片操作,input的标签有一个accept属性,accept 属性只能与 <input type="file"> 配合使用.它规定能够通过文 ...

  2. PHP正则提取或替换img标记属性实现文章预览

    今天在想如何实现文章预览时,如果文章里面包含照片,那么就选取第一张照片作为预览图,如果没有照片,则截取文章的头150个字作为预览文字,但是因为保存在数据库的文章都是以富文本的形式,没办法直接提取,在网 ...

  3. 网页中动态嵌入PDF文件/在线预览PDF内容https://www.cnblogs.com/xgyy/p/6119459.html

    #网页中动态嵌入PDF文件/在线预览PDF内容# 摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如 ...

  4. KindEditor的简单使用,以及上传图片预览图片,用户删除图片后的数据处理(重点),以及 BeautifulSoup,shutil两模块了解

    KindEditor的简单了解 http://www.cnblogs.com/wupeiqi/articles/6307554.html 简单使用: <div class="comm& ...

  5. JS图片上传预览插件制作(兼容到IE6)

    其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方 ...

  6. 【JS/CSS3】实现带预览图幻灯片效果~

    一.前期准备 1.1 案例分析 适用场景:单例布局1.2 方法论 V视图 HTML+CSS+调试C js实现控制流程D数据 优化扩展 二.代码 结构 <div class="slide ...

  7. 实战动态PDF在线预览及带签名的PDF文件转换

    开篇语: 最近工作需要做一个借款合同,公司以前的合同都是通过app端下载,然后通过本地打开pdf文件,而喜欢创新的我,心想着为什么不能在线H5预览,正是这个想法,说干就干,实践过程总是艰难的,折腾了3 ...

  8. dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

    http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...

  9. FlexPaper+SWFTool+操作类=在线预览PDF

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

随机推荐

  1. CodeForces - 393E Yet Another Number Sequence

    Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  2. redis 延迟消息

    1.查询下redis 是否打开了键空间通知功能 发现打开了,如果没有打开可以在执行下 我们可以看到参数设置 2.订阅下键空间或者事件通知 订阅键空间:subscribe __keyspace@0__: ...

  3. SG函数学习总结

    有点散乱, 将就着看吧. 首先是博弈论的基础, 即 N 和 P 两种状态: N 为必胜状态, P 为必败状态. 对于N, P两种状态, 则有 1. 没有任何合法操作的状态, P; 2. 可以移动到P局 ...

  4. 随想录(fatfs的学习)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上学的时候就对文件系统非常有兴趣.可是苦于没有合适的fs代码能够学习.市面上的fs代码,要么太 ...

  5. angular - 编辑html文件-4

    启动服务器: angular默认端口:4200 ng serve --port 3000 --open 输入本条命令后,会自动打开默认浏览器以及打开APP页 推荐开发工具webStorm,全平台兼容M ...

  6. OA权限树搭建 代码

    <ul id="tree"> <s:iterator value="#application.topPrivilegeList"> &l ...

  7. 高速创建和mysql表相应的java domain实体类

    今天创建了一个表有十几个字段,创建完之后必定要写一个与之相应的java domain实体类. 这不是反复的工作吗?为什么不先把这个表的全部的字段查出来,然后放到linux环境下,用sed工具在每一行的 ...

  8. SDUT 1068-Number Steps(数学:直线)

    Number Steps Time Limit: 1000ms   Memory limit: 10000K  有疑问?点这里^_^ 题目描写叙述 Starting from point (0,0) ...

  9. 转:scanf的用法

    https://blog.csdn.net/u012421456/article/details/18501309 scanf()[通过键盘将数据输入到变量中] 它有两种用法: 用法一: scanf( ...

  10. MapReduce框架在Yarn上的具体解释

    MapReduce任务解析 在YARN上一个MapReduce任务叫做一个Job. 一个Job的主程序在MapReduce框架上实现的应用名称叫MRAppMaster. MapReduce任务的Tim ...