修改富文本里边的图片链接
本文将介绍富文本里面的图片地址进行修改,我们可以通过正则的方式来对富文本的图片进行一次精准的检索,之后 同样采用匹配替换。为什么要这么做呢,因为有时候可能在上传富文本图片的的时候可能没有将域名存进去或者更换了域名需要进行域名绑定或者更替则可以用到。下面是实现的代码
function preg_match_all_img(&$content){ preg_match_all("/<img(.*)src=\"([^\"]+)\"[^>]+>/isU",$content,$matches); if(!empty($matches)){ $img = $matches[2]; }else{ $img = ""; } if(!empty($img)){ $host_url = "http://".$_SERVER['SERVER_NAME']; $patterns = array(); $replacements = array(); foreach($img as $imgitem){ $final_imgUrl = $host_url.$imgItem; $replacements[] = $final_imgUrl; $img_new = "/".preg_replace("/\//i","\/",$imgItem)."/"; $patterns[] = $img_new; } ksort($patterns); ksort($replacements); $content = preg_replace($patterns,$replacements,$content); } }
版权声明:本文由“憨小猪”发布,如需转载请注明出处。