富文本中的路径加上域名
//富文本内容替换 $content 富文本内容 $strUrl 域名
function replacePicUrl($content = null, $strUrl = null) {
if ($strUrl) {
$search = '/(src)=([\"|\'])?(.*?)(?(2)\2|\s)/is';
preg_match_all($search, $content , $matches);
//var_dump($matches);
$src = "";
if(!empty($matches)) {
//注意,上面的正则表达式说明src的值是放在数组的第三个中
$src = $matches[3];
}else {
$src = "";
}
if (!empty($src)) {
$patterns= array();
$replacements = array();
foreach($src as $imgItem){
//检测是否已含域名
if(!filter_var($imgItem, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
$final_imgUrl = $strUrl.$imgItem;
}else{
$final_imgUrl = $imgItem;
}
$replacements[] = $final_imgUrl;
$img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";
$patterns[] = $img_new;
}
//让数组按照key来排序
ksort($patterns);
ksort($replacements);
//替换内容
$vote_content = preg_replace($patterns, $replacements, $content);
return $vote_content;
}else {
return $content;
}
} else {
return $content;
}
}
|