在做響應(yīng)式或者手機(jī)站的模板時(shí)候,我們經(jīng)常會(huì)遇到圖片因?yàn)殚L度和寬度固定,而無法自適應(yīng)手機(jī)端的問題,原因就是是織夢(mèng)的編輯器在上傳圖片時(shí),會(huì)自動(dòng)加上style屬性,因此,在保存文章時(shí),我們需要先清除掉這些屬性。跟版網(wǎng)的小編最近也遇到這個(gè)問題,百度上搜了一下,有一種方法就是對(duì)body中的數(shù)據(jù)進(jìn)行過濾,寫法如下(在內(nèi)容頁找到{dede:field.body}修改為如下內(nèi)容):
{dede:field.body runphp=yes}
global $cfg_basehost;
$str = @me;
$search = '/(<img.*?)width=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$search1 = '/(<img.*?)height=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$search2 = '#(<img.*?style=".*?)width:\d+px;([^"]*?.*?>)#i';
$search3 = '#(<img.*?style=".*?)height:\d+px;([^"]*?.*?>)#i';
$content = preg_replace($search,'$1$3',$str);
$content = preg_replace($search1,'$1$3',$content);
$content = preg_replace($search2,'$1$2',$content);
$content = preg_replace($search3,'$1$2',$content);
@me = $content;
//@me = str_replace('/uploads/allimg/', $cfg_basehost.'/uploads/allimg/', $content);//手機(jī)版圖片使用絕對(duì)路徑
{/dede:field.body}
AB織夢(mèng)模板網(wǎng)試了一下,我復(fù)制的內(nèi)容中還有一些雜項(xiàng)其實(shí)還是沒有過濾掉的。于是想到直接在保存時(shí)候進(jìn)行處理,找到:/dede/article_add.php和/dede/article_edit.php 這兩個(gè)文件(對(duì)應(yīng)文章模型,其他模型請(qǐng)找后臺(tái)對(duì)應(yīng)的文件),搜索如下代碼:
$body = AnalyseHtmlBody($body,$description,$litpic,$keywords,'htmltext');
在這段代碼的后面加入:
//去除img中的style屬性
$body = preg_replace("/style=\\\.+?['|\"]/i",'',$body);
//去除img中的width,height屬性
$exp=Array("/height=.{0,5}\s/i","/width=.{0,5}\s/i"); $exp_o=Array('','');
$body = preg_replace($exp,$exp_o,$body);
其實(shí)只改一個(gè)article_edit.php文件就行了,修改后發(fā)布或者修改文檔時(shí)候,會(huì)自動(dòng)去除掉body中的所有style。后者會(huì)把內(nèi)容中所有的style都會(huì)被去掉,文檔相當(dāng)純凈。
具體用哪種方法,大家可以根據(jù)需求而定。