最近没事,打算把自己的小项目改造为mip,进行测试学习,想把资讯栏目:http://zhimo.yuanzhumuban.cc/news/。全部改造为mip。但是MIP改造一项是:图片标签的改造。而且dt内核跟其他不一样,不像wordpress和织梦一样简单,网上的资料也很少,所以本人就自己写了一个正则替换,希望对大家MIP改造有所帮助。本代码由我自己原创,如有更好的方法,可以一起交流,可以关注我的网站:http://zhimo.yuanzhumuban.cc/
废话不多说,开始!
=======================================================================================================================
首先第一步,找到你修改的模块,我以资讯模块为例:
路径为:/module/article/show.inc.php
然后打开,大概在18行左右 插入一下代码,进行正则替换
function mip_replace($content = ''){ $pattern1 = "#<img.*?src=['"](.*?)['"].*?>#ims"; $imgcontent=array(); preg_match_all($pattern1,$content,$img); $imgcontent = $img[0]; $imgurl = $img[1]; foreach($imgcontent as $imgk=>$imgv) { $temp = str_replace('<img','mip-img',$imgv); $temp = str_replace('/>','></mip-img',$temp); $url = $imgurl[$imgk]; $url = mip_format_img_url($url); $temp = preg_replace("/src=['"].*?['"]/si","src="$url"",$temp); $mipimg[$imgk] = $temp; } $content = preg_replace($imgcontent,$mipimg,$content); $content =preg_replace("/<a /si","<a target="_blank" ",$content); $content =preg_replace("/style=".*?"/si","",$content); return mip_utf8($content); } function mip_format_img_url( $url = ''){ if(stripos($url, 'http') === 0 || stripos($url, 'ftp') === 0 ){ return $url; } if(stripos($url, '/') === 0){ $url = 'http://'.$_SERVER['HTTP_HOST'].$url; }else{ $url = 'http://'.$_SERVER['HTTP_HOST'].'/'.$url; } return $url; } function mip_utf8($string = '') { $fileType = mb_detect_encoding($string , array('UTF-8','GBK','LATIN1','BIG5')); if( $fileType != 'UTF-8'){ $string = mb_convert_encoding($string ,'utf-8' , $fileType); } return $string; } $content=mip_replace($content);
这样就好了,以上就是个人的写法,如有其他好方法可以分享交流!