小程序解析富文本图片半路劲不显示的解决方案!PHP版
辛辛苦苦吧富文本解析到小程序了,但是好多图片是办路径的,显示不出来怎么办呢?
这就需要用到世界上最好用的PHP语言了
下面直击上方法
/**
* 替换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;
}
}
上述是使用的正则替换的方式,当然你有其他方式替换也是可以的
//读取文章详情
public function detail()
{
$id = Request::instance()->param("id");
$res = \app\common\model\Article::with("user")->where( "id",$id)->find();
$content = replacePicUrl($res->content,"https://admin.trianglefit.top/");
$res->content = $content;
if($res){
return json(["code"=>200,"msg"=>"成功","data"=>$res]);
}else{
return json(["code"=>201,"msg"=>"暂无文章","data"=>""]);
}
}
感谢:https://myfreespace.iteye.com/blog/1078303
软件下载链接
点击下载(1积分)
如何下载
积分获取攻略
下载说明:本站软件全部都是由个收集,绝对无毒无公害!如若有侵权或,请联系qq571031767进行删除!
软件1群:209183654 2群:524440238 3群:397053211 4群:684245561