百度编辑器内容之MIP图片替换,style提取
来源:互联网 发布:centos 离线安装samba 编辑:程序博客网 时间:2024/06/07 10:24
平台使用ectouch做站,MIP出来了一段时间,推广要求把资讯单独提出改造成MIP,所以把资讯列表和资讯详情根据MIP要求进行了改造,但是百度编辑器编辑出来的内容图片需要替换,style也需要提取,我对正则表达式也不熟悉,在网上找了些资源,在此谢谢各位博客君。
资源链接:ectouch之新闻页面MIP化
替换图片使用到的函数:
function replacePicUrl($content = null, $url="") {
$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = "<mip-img src={$url}$3.$4></mip-img>";
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
有点图片有title,class之类的,可以用下面这个
function replacePicUrl($content = null, $url="") {
//$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.jpeg]|\.png]|\.bmp]))[\'|\"].*?[\/]?>/";
preg_match_all($pattern, $content,$matches);
$full_img = $matches[0];
$full_src = $matches[1];
foreach ($full_img as $k => $v) {
$v1 = str_replace("<img", "<mip-img", $v);
$v1 = str_replace("/>", "></mip-img>", $v1);
$new_path = $url.$full_src[$k];
$v1 = str_replace($full_src[$k], $new_path, $v1);
$content = str_replace($v, $v1, $content);
}
return $content;
}
提取style:
function getStyle($content = null){
preg_match_all("/style=('|\")([^'\"]+)('|\")/",
$content,$matches);
$styles = $matches[0];
$styles_value = $matches[2];
$style_custom = "";
$i = 0;
foreach($styles_value as $key){
$style_custom .= ".class".$i."{".$key."}";
$class_name = 'class="class'.$i.'"';
$replacements = $class_name;
$patterns = $styles[$i];
$content = str_replace($patterns, $replacements, $content);
$i++;
}
$res['style_custom'] = $style_custom;
$res['content'] = $content;
return $res;
}
在controller页面中做替换:
$article = model('Article')->get_article_info($article_id);
$article_content = replacePicUrl($article['content'], "http://www.jxcat.com"); //替换图片
//提取style
$style_res = getStyle($article_content);
$article_content = $style_res['content'];
$style_custom = $style_res['style_custom'];
$article['content'] = $article_content;
$this->assign('article', $article);
$this->assign('style_custom', $style_custom);
- 百度编辑器内容之MIP图片替换,style提取
- 百度编辑器图片被替换
- 百度编辑器之上传图片
- 百度MIP-学习地址
- 百度MIP组件使用方法:mip-list mip-infinitescroll等
- 富文本编辑器内容替换
- 基于phpcms的百度mip内容转换方法(适用任何cms)
- 百度MIP校验错误整理与mip规则
- 图片上传功能之~百度编辑器单独作为图片上传文件上传功能,百度编辑器做缩略图
- 百度推出MIP Shell链接MIP URL以及 MIP-Shell UR
- 百度编辑器,图片上传优化
- 图片自适应大小@百度编辑器
- 百度编辑器上传图片至七牛云
- RF之百度编辑器
- 百度MIP计划,移动站的首选
- 1145.cn 百度MIP适配实例
- asp文本编辑器/数据库中提取图片
- vi编辑器之替换操作
- Java调用极光推送推送消息
- Next Greater Element I
- kaldi feature extraction
- 可持久化并查集(一)——从阿克曼函数到镜像
- FLV学习(三)FlvParser源码阅读(1)总体流程
- 百度编辑器内容之MIP图片替换,style提取
- Maven项目错误总汇
- Spark笔记二之Standalone模式
- centos系统添加/删除用户和用户组的例子
- 文章标题
- 基于DDD的.NET开发框架ABP实例,多租户 (Sass)应用程序,采用.NET MVC, Angularjs, EntityFramework-介绍
- 支付宝和Ping++的包冲突问题:Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files defin
- OSGI入门helloworld实例
- Git版本控制工具结合GitHub从入门到精通常用命令学习手册