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. PyTorch学习笔记之初识word_embedding

    import torch import torch.nn as nn from torch.autograd import Variable word2id = {'hello': 0, 'world ...

  2. 管理SQL Server监控

    http://blog.csdn.net/DBA_Huangzj/article/category/1133081 http://www.cnblogs.com/bhtfg538/archive/20 ...

  3. javascript --- 递归的简单理解

    递归函数大家都应该比较熟吧?那么,如何在JavaScript中书写一个完美的递归函数呢?且听我娓娓道来. 递归函数 写的时候,查了一下维基百科对递归函数的定义,恕我愚钝,简直太深奥了!所以,我还是简单 ...

  4. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  5. 【java】spring项目中 对entity进行本类间的克隆

    方法1: [使用spring自带BeanUtils实现克隆] [要求:需要被克隆的类实现Cloneable接口并且重写clone()方法] >例子: >>实体: package co ...

  6. [剑指Offer]2.变态跳台阶

    题目 一仅仅青蛙一次能够跳上1级台阶,也能够跳上2级--它也能够跳上n级. 求该青蛙跳上一个n级的台阶总共同拥有多少种跳法. 思路 用Fib(n)表示青蛙跳上n阶台阶的跳法数,设定Fib(0) = 1 ...

  7. libevent和libev的区别对比

    参考了这篇文章: http://www.cnblogs.com/Lifehacker/p/whats_the_difference_between_libevent_and_libev_chinese ...

  8. Hadoop 2.2.0集群搭建

    一.环境说明 1.虚拟机平台:VMware10 2.Linux版本号:ubuntu-12.04.3-desktop-i386 3.JDK:jdk1.7.0_51 4.Hadoop版本号:2.2.0 5 ...

  9. XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。

    示例Xml: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml versio ...

  10. pyquery库的使用

    pyquery标签选择 获取了所有的img标签(css选择器,你也可以换成不同的class和id) import requests import re from pyquery import PyQu ...