有小伙伴问关于老牌CMS - yourphp的多图上传中的图片无法单张调用,看了下数据库,发现yourphp的多图上传是将多张图片的地址跟图片名称组合成一个字符串存入数据库中的,如图:
所以当从数据库中调用数据时,需要对调用出来的多图数据进行处理下,处理的方法有很多种,博主在这里采用的是进行字符串分割,通过字符串的组合形式来进行分割,代码如下:
方法一
PHP
<?php $ar = '/Uploads/201802/5a730bfbc1889.jpg|niceboy.jpg:::/ Uploads/201811/5a730bfbed3cb.gif|pjaxLoad.gif:::/ Uploads/201811/5a730bfc26826.png|捕获.PNG:::/ Uploads/201811/5a730bfc440d4.png|捕获1.PNG:::/ Uploads/201811/5a730bfc26826.png|捕获.PNG:::/ Uploads/201811/5a730bfc440d4.png|捕获1.PNG:::/ Uploads/201811/5a730bfc5fa43.png|捕获2.PNG'; function imgUrl($ar){ $re = explode('|', $ar);//这里可以先以:来分割,explode(':',$ar); foreach ($re as $key => $value) { if($key != 0){ $nre = explode(':', $value); $narray[] =$nre; }else{ $result[] = $re[0]; } } for($i = 0 ;$i<count($narray);++$i){ $combine[] = array_column($narray, $i);} $newCombine = array_merge_recursive($combine[3], $result) ; return $newCombine;} echo "<pre>"; print_r(imgUrl($ar));?>
方法二
PHP
<?php $ar = '/Uploads/201811/5a730bfbc1889.jpg|niceboy.jpg:::/ Uploads/201811/5a730bfbed3cb.gif|pjaxLoad.gif:::/ Uploads/201811/5a730bfc26826.png|捕获.PNG:::/ Uploads/201811/5a730bfc440d4.png|捕获1.PNG:::/ Uploads/201811/5a730bfc26826.png|捕获.PNG:::/ Uploads/201811/5a730bfc440d4.png|捕获1.PNG:::/ Uploads/201811/5a730bfc5fa43.png|捕获2.PNG';function imgUrl($ar){ $re = explode(':', $ar);//这里可以先以:来分割,explode(':',$ar); foreach ($re as $key => $value) { $nre = explode('|', $value); $narray[] =array_filter($nre); } for($i = 0 ;$i<count($narray);++$i){ $combine[] = array_column($narray, $i); } return $combine[0];} echo "<pre>"; print_r(imgUrl($ar));?>
通过运行以上代码,可以得出一个含有纯图片地址的数组
Array( [0] => /Uploads/201811/5a730bfbed3cb.gif [1] => /Uploads/201811/5a730bfc26826.png [2] => /Uploads/201811/5a730bfc440d4.png [3] => /Uploads/201811/5a730bfc5fa43.png [4] => /Uploads/201811/5a730bfbc1889.png )
微信扫码查看本文
发表评论