在写ecshop接口时,如果详情页的图片是上传到本地的,可能就显示不出图片:如images/upload/Image/dao2_01.jpg,需要写成如http://www.csdn.com/images/upload/Image/dao2_01.jpg这样才能显示出来,所以需要取出详情页的图片并判断是否远程图片,不是的话就需要给图片加上网站域名。
function replacePicUrl($content = null, $strUrl = null) {
if ($strUrl) {
preg_match_all("/<img([^>]*)\s*src=('|\")([^'\"]+)('|\")/",
$content,$matches);//带引号
//preg_match_all("/<img([^>]*)\ssrc=([^\s>]+)/",$string,$matches);//不带引号
$new_arr=array_unique($matches[3]);//去除数组中重复的值
$patterns= array();
$replacements = array();
foreach($new_arr as $key){
$final_imgUrl = (strpos($key, 'http://') === false && strpos($key, 'https://') === false) ? $strUrl.$key : $key;
$replacements[] = $final_imgUrl;
$img_new = "/".preg_replace("/\//i","\/",$key)."/";
$patterns[] = $img_new;
}
//让数组按照key来排序
ksort($patterns);
ksort($replacements);
//替换内容
$new_content = preg_replace($patterns, $replacements, $content);
return $new_content;
} else {
return $content;
}
}