DEDECMS织梦文章内容图片img转mip-img

时间:2019-04-14 23:00 / 来源:未知 / 作者:酱油 / 点击:

如果使用FCK编辑器,可以直接在里修改【完美解决FCK编辑器上传图片自动带DIV标签的问题】,但是存在一个问题,那就是在编辑器里预览不了,以下的方法更加简单,直接替换img代码为mip-img.

打开/include/extend.func.php,在下面添加如下代码,修改好代码后,在模板里面调用:{dede:field name='body' function='mipBody(@me)'/}这样我们就成功的替换了img为img-mip

function mipBody($body)
{
$body = str_replace(' style="white-space:pre"', '', $body);
preg_match_all('/<img (.*?)\>/', $body, $images);
if (!is_null($images)) {
foreach ($images[1] as $index => $value) {
$mip_img = str_replace('<img', '<mip-img', $images[0][$index]);
$mip_img = str_replace('>', '></mip-img>', $mip_img);
$mip_img = preg_replace('/(width|height)="\d*"\s/', '', $mip_img);
$mip_img = preg_replace('/ style=\".*?\"/', '', $mip_img);
$mip_img = preg_replace('/ class=\".*?\"/', '', $mip_img);
$body = $content = str_replace($images[0][$index], $mip_img, $body);
}
}
return $body;
}


题外话:因为网站的需要,之前修改了编辑器,在手动做链接的时候,希望能从<a herf="xxx">yyy</a>转为mip格式,所以在上面的基础上再稍微修改下,仅此举例。

function mipBody($body)
{
$body = str_replace(' style="white-space:pre"', '', $body);
preg_match_all('/<img (.*?)\>/', $body, $images);
if (!is_null($images)) {
foreach ($images[1] as $index => $value) {
$mip_img = str_replace('<img', '<mip-img', $images[0][$index]);
$mip_img = str_replace('>', '></mip-img>', $mip_img);
$mip_img = preg_replace('/(width|height)="\d*"\s/', '', $mip_img);
$mip_img = preg_replace('/ style=\".*?\"/', '', $mip_img);
$mip_img = preg_replace('/ class=\".*?\"/', '', $mip_img);
$body = $content = str_replace($images[0][$index], $mip_img, $body);
}
}
preg_match_all('/<a (.*?)>/', $body, $imagess);
if (!is_null($imagess)) {
foreach ($imagess[1] as $index => $value) {
$mip_imgs = str_replace('<a href', '<a data-type="mip" href"', $imagess[0][$index]);
$body = $content = str_replace($imagess[0][$index], $mip_imgs, $body);
}
}
return $body;
}


猜你喜欢

推荐栏目

DEDECMS
DEDECMS

DEDE使用经验和技巧

WordPress
Wordpress

WP使用经验和技巧

Zblog
Zblog

Zblog使用经验和技巧

CSS
CSS

CSS使用经验和技巧

网络资料
网络资料

转载实用网络资料

按键伤人
按键伤人

转载乱七八糟的网络资源

我是酱油
我是酱油

随笔札记 记录生活点滴

本站推荐

百度站内搜索

专业博客主机