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 ...
随机推荐
- 51Nod 约数之和
1220 约数之和 题目来源: Project Euler 基准时间限制:3 秒 ...
- JDK1.8和Spring 3.2.0 的坑
上午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending context initialized e ...
- Extjs grid增加或删除列后记住滚动条的位置
IE下验证好使. { text: "Del", icon: 'Scripts/Ext/resources/images/icons/application_form_delete. ...
- CreateJS结合Falsh工具生成Canvas动画(加密字符串的由来)
CreateJS是一款制作Canvas动画的工具. 官网如下: http://www.createjs.com/ http://www.createjs.cc/ 使用CreateJS时最大的疑问就是J ...
- 10.【nuxt起步】-引用mintui
这时候我们完成了list.vue,但是怎么返回index.vue,这时候需要这个头部返回 1.我们使用现成的minu-ui,eleme的开源移动端 ,参考 https://www.cnblogs.co ...
- 【转载】使用事件模型 & libev学习
参考这篇文章: http://www.ibm.com/developerworks/cn/linux/l-cn-edntwk/ 这里面使用的是 libev ,不是libevent Nodejs就是采用 ...
- TCP三次握手与DDOS攻击原理
TCP三次握手与DDOS攻击原理 作者:冰盾防火墙 网站:www.bingdun.com 日期:2014-12-09 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. ...
- 关于Adapter对数据库的查询、删除操作
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- MySQL 查询某个列中同样值的数量统计
数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...
- 怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优
怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优 1>.这里简单举个样例来说明DBMS_SQLTUN ...