
发表时间:2019-01-24 14:03:46 浏览次数:2416
传统的PC管理后台一般保存的都是图片路径,随着移动端APP的流行,很多平台需要增加API接口。这时发现图片路径并不能被远程读取,下面提供图片路径转换为URL地址的方法:
/**
* 替换fckedit中的图片 添加域名
* @param string $content 要替换的内容
* @param string $strUrl 内容中图片要加的域名
* @return string
* @eg
*/
function replacePicUrl($content = null, $strUrl = null) {
if ($strUrl) {
//提取图片路径的src的正则表达式 并把结果存入$matches中
preg_match_all("/]+>/isU",$content,$matches);
$img = "";
if(!empty($matches)) {
//注意,上面的正则表达式说明src的值是放在数组的第三个中
$img = $matches[2];
}else {
$img = "";
}
if (!empty($img)) {
$patterns= array();
$replacements = array();
foreach($img as $imgItem){
$final_imgUrl = $strUrl.$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;
}
}上面是网上提供的方法,已经接近完美,但唯一的遗憾时不能判断图片地址是否包含域名,也就是说不管图片地址包不包含域名都添加域名,因此把
$final_imgUrl = $imgItem;
替换为如下代码:
if(!filter_var($imgItem, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
$final_imgUrl = $strUrl.$imgItem;
}else{
$final_imgUrl = $imgItem;
}道理很简单,用filter_var函数通过FILTER_VALIDATE_URL过滤器过滤变量。最后达到的效果是:图片地址包含域名则添加域名、不包含域名则不处理。
微信扫码支付
时间:2019-12-03 22:35:13 浏览:2289
时间:2014-03-31 14:05:43 浏览:575
时间:2019-01-18 22:02:45 浏览:769
时间:2019-01-18 22:15:14 浏览:996
时间:2019-01-18 22:11:32 浏览:580
时间:2019-01-25 15:18:33 浏览:698
时间:2020-01-06 14:28:54 浏览:495
时间:2019-01-18 22:01:38 浏览:485
时间:2019-01-18 22:05:34 浏览:583
时间:2019-02-22 10:19:56 浏览:695