mip是百度独家推出的一套网页语法,通过引入百度mip文件,可以实现网页移动端速度加快。并且因为之前百度对经过mip改造过的网站会有一个闪电标志,并且据说可以获得排名优先特权,所以在站长圈风靡一时。
织梦cms能不能改造成mip网站呢?那是必须可以的,因为所谓的mip网站不过是改造了一下模板而已。通过修改模板,可以实现前台的dedecms引入百度mip的目的。但是,只是修改模板的话,文章中插入的图片就不是很好实现mip化了,因为图片的引用img标签是固定在数据库中的,而模板之中自动把它替换成mip-img比较困难。今天来讲一下,dedecms文章中imp标签实现mip化的方法。此方法可以为织梦cms新增一个自定义的文章内容覅用标签,从而实现mip站改造之后不影响pc站和说基站的效果。
<img 标签替换成 <mip-img标签
width|height 删除掉,去除图片宽高
style 内联样式删除掉
/uploads/ 把图片的相对路径替换成绝对路径
打开 /include/extend.func.php在它的最下面另起一行加入下面代码
/** * dedecms文章内图片适配百度MIP规范 * * @access public * @param string $content 文章内容 * @return string */function mip($content){ global $cfg_basehost; preg_match_all('/<img (.*?)\>/', $content, $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); $content = str_replace($images[0][$index], $mip_img, $content); } } preg_match_all('/ style=\".*?\"/', $content, $style); if(!is_null($style)) { foreach($style[0] as $index => $value){ $mip_style = preg_replace('/ style=\".*?\"/', '',$style[0][$index]); $content = str_replace($style[0][$index], $mip_style, $content); } } $content = str_replace('/uploads/', $cfg_basehost.'/uploads/', $content); return $content; }
{dede:field.body function=mip(@me)/}