php调用部分图片不显示,小程序解析富文本图片半路劲不显示的解决方案!PHP版...

小程序解析富文本图片半路劲不显示的解决方案!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("/

343.html]+>/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值