聯(lián)系官方銷售客服
1835022288
028-61286886
想實(shí)現(xiàn)把文章里的圖片為絕對(duì)路徑,又同時(shí)去除img中的style。求大神幫忙!
{php echo preg_replace('/(<img.*?)(style=.+?[\'|"])/i','$1',$content);} 這個(gè)是去除img里的style
{php echo preg_replace('/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i',"<img src=\"".SITE_URL."$1\">", $content);} 這個(gè)是圖片替換為絕對(duì)路徑
以上兩個(gè)表達(dá)式單獨(dú)放一個(gè)都沒啥問題,但兩個(gè)放一塊就只能實(shí)現(xiàn)一個(gè)功能,有大神知道如何同時(shí)實(shí)現(xiàn)以上兩個(gè)功能嗎?表達(dá)式應(yīng)該怎么寫?
在這個(gè)字段,的數(shù)據(jù)驗(yàn)證里有個(gè)過濾函數(shù),你把上面的寫成函數(shù)調(diào)用一下就好了
參考文檔:《自定義過濾函數(shù)》
這是php基礎(chǔ)語法
1、打開 config/custom.php 添加自定義函數(shù)
/*
* 文檔內(nèi)容-圖片去寬高自適應(yīng)-圖片絕對(duì)路徑
* @param string $body
* @return string 返回內(nèi)容
*/
if(!function_exists('resetBody'))
{
function resetBody($body)
{
$search = '/(<img.*?)width=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$search1 = '/(<img.*?)height=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is';
$search2 = '#(<img.*?style=".*?)width:\s{0,}\d+px;([^"]*?.*?>)#i';
$search3 = '#(<img.*?style=".*?)height:\s{0,}\d+px;([^"]*?.*?>)#i';
$content = preg_replace($search, '$1$3', $body);
$content = preg_replace($search1, '$1$3', $content);
$content = preg_replace($search2, '$1$2', $content);
$content = preg_replace($search3, '$1$2', $content);
$result = str_replace('/uploads/', SITE_URL . '/uploads/', $content);
return $result;
}
}
2、模板調(diào)用文檔內(nèi)容標(biāo)簽
{$content}
改成
{resetBody($content)}