HTML预览 正则替换
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预览 正则替换的更多相关文章
- webform的原生操作图片预览和上传
1.使用input标签进行图片操作,input的标签有一个accept属性,accept 属性只能与 <input type="file"> 配合使用.它规定能够通过文 ...
- PHP正则提取或替换img标记属性实现文章预览
今天在想如何实现文章预览时,如果文章里面包含照片,那么就选取第一张照片作为预览图,如果没有照片,则截取文章的头150个字作为预览文字,但是因为保存在数据库的文章都是以富文本的形式,没办法直接提取,在网 ...
- 网页中动态嵌入PDF文件/在线预览PDF内容https://www.cnblogs.com/xgyy/p/6119459.html
#网页中动态嵌入PDF文件/在线预览PDF内容# 摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如 ...
- KindEditor的简单使用,以及上传图片预览图片,用户删除图片后的数据处理(重点),以及 BeautifulSoup,shutil两模块了解
KindEditor的简单了解 http://www.cnblogs.com/wupeiqi/articles/6307554.html 简单使用: <div class="comm& ...
- JS图片上传预览插件制作(兼容到IE6)
其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方 ...
- 【JS/CSS3】实现带预览图幻灯片效果~
一.前期准备 1.1 案例分析 适用场景:单例布局1.2 方法论 V视图 HTML+CSS+调试C js实现控制流程D数据 优化扩展 二.代码 结构 <div class="slide ...
- 实战动态PDF在线预览及带签名的PDF文件转换
开篇语: 最近工作需要做一个借款合同,公司以前的合同都是通过app端下载,然后通过本地打开pdf文件,而喜欢创新的我,心想着为什么不能在线H5预览,正是这个想法,说干就干,实践过程总是艰难的,折腾了3 ...
- dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...
- FlexPaper+SWFTool+操作类=在线预览PDF
引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...
随机推荐
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...
- 洛谷1967货车运输 即 NOIP2013 DAY1 T3
题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...
- Maven更新POM中的JDK版本(比如更新为JDK1.8)
默认POM如果不指定JDK版本为1.5,而有些项目需要使用泛型这些,就必须使用1.8版本的JDK,所以需要手动修改POM. 而所涉及到的还是插件maven-compiler-plugin,官方参考:h ...
- SQL Server 存储
http://baoqiangwang.blog.51cto.com/1554549/541298/
- 提高在Xcode上的工作效率
对于在Xcode上提高工作效率,内功在这不提,对于外力,我将它分为三类: 工具.快捷键和小技巧.主要获得的路径是通过平时积累和看 WWDC12 上的 Session 402:Working Effic ...
- Code Sign error: a valid provisioning profile matching the application's Identifier 'com. sensoSource.VoiceRecorder' could not be found
如果不是com. sensoSource.VoiceRecorder,在xxx-info.plist里Bundle identifier里替换成你的证书名 xxx是你的工程名 在Bundle iden ...
- 如果当前地图文档中有独立的Table,通过Engine如何获取该Table?
将IMap转为ITableCollection,通过ITableCollection.get_Table(int index);来获取该Table
- Office 顿号怎么输
中文状态下回车上面一个按键就是
- 辛星浅析raid
我们来分析一下影响计算机性能的主要组件,主要就是CPU.主板的总线IO.内存IO.硬盘IO.网卡IO等等,并且如今CPU的性能已经非常好了,可是计算机的总体的IO性能却较低,严重影响了计算机的性能,如 ...
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...