想實現(xiàn)把文章里的圖片為絕對路徑,又同時去除img中的style。求大神幫忙!
{php echo preg_replace('/(<img.*?)(style=.+?[\'|"])/i','$1',$content);} 這個是去除img里的style
{php echo preg_replace('/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i',"<img src=\"".SITE_URL."$1\">", $content);} 這個是圖片替換為絕對路徑
以上兩個表達(dá)式單獨放一個都沒啥問題,但兩個放一塊就只能實現(xiàn)一個功能,有大神知道如何同時實現(xiàn)以上兩個功能嗎?表達(dá)式應(yīng)該怎么寫?
在這個字段,的數(shù)據(jù)驗證里有個過濾函數(shù),你把上面的寫成函數(shù)調(diào)用一下就好了
參考文檔:《自定義過濾函數(shù)》
{php $content = preg_replace('/(<img.*?)(style=.+?[\'|"])/i','$1',$content);} {php echo preg_replace('/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i',"<img src=\"".SITE_URL."$1\">", $content);}這是php基礎(chǔ)語法
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
1、打開 config/custom.php 添加自定義函數(shù)
/*
* 文檔內(nèi)容-圖片去寬高自適應(yīng)-圖片絕對路徑
* @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)}