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 ...
随机推荐
- 2017 [六省联考] T6 寿司餐厅
4873: [Shoi2017]寿司餐厅 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 450 Solved: 316[Submit][Status ...
- PropertyPlaceholderConfigurer 基本用法
目录 一.PropertyPlaceholderConfigurer 的继承体系 二.PropertyPlaceholderConfigurer 的基本概念 三.PropertyPlaceholder ...
- spring beans 接口
- Ubuntu 16.04中的Dock的应用顺序调整
操作步骤: 参考: https://askubuntu.com/questions/39805/is-there-an-easy-way-to-rearrange-or-move-the-icons- ...
- 从顺序随机I/O原理来讨论MYSQL MRR NLJ BNL BKA
http://blog.itpub.net/7728585/viewspace-2129502/
- golang 版本升降之后报错——imports runtime: C source files not allowed when not using cgo or SWIG
问题: golang 升级或者降级版本之后,执行编译报错如下: package github.com/onsi/ginkgo/ginkgo imports runtime: C source file ...
- 嵌入式Linux驱动案例之中的一个
前几天解决一个嵌入式Linux驱动问题,做为一个案例进行记录. 本案例是一个CPU通过LocalBus总线訪问外围一个设备,详细设备是一个DSP器件.在实际应用中,性能要求非常高,对数据訪问速度提出比 ...
- ListView中button监听器 设置 及 优化
在应用开发中常常会用到ListView,而且每个Item里面都会有button之类的须要进行事件监听的控件.在给button加入OnClickListener的时候,一開始非常下意识的会想在ListV ...
- OpenCV load 运行出错 cv::Exception 出错
今天在使用OpenCV的load函数时,出现错误,代码如下: cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); ...
- 【网络协议】TCP的流量控制机制
一般来说,我们总是希望传输数据的更快一些,但假设发送方把数据发送的非常快.而接收方来不及接收,这就可能造成数据的丢失.流量控制就是让发送方的发送速率不要太快.让接收方来得及接收. 对于成块数据流,TC ...