【问题标题】:using explode in php to separate a string of multiple images在php中使用explode来分隔多个图像的字符串
【发布时间】:2013-10-18 22:30:53
【问题描述】:

我正在从数据库中选择行并使用以下内容分隔每个图像(全部在一列中)

$images = explode(',',$result['images']);

然后显示一张图片,我正在使用这个 HTML / PHP 代码:

<img src="/img/project-gallery/'.strtr($images[0],'[]','""').'"

但它的显示如下所示

<img src="/img/project-gallery/" 5_b.jpg""="">

我怎样才能让它像图像一样显示

在我的数据库中,图像都在一个列中,例如:

[image1.jpg],[image2.jpg],[image3.jpg] 等等...

【问题讨论】:

  • 为什么不直接做substr($images, 1, -1)
  • 我怀疑逗号后面有空格,你需要删除它们。另外,你不应该用引号替换括号,你应该删除它们。
  • ...我的意思是循环中的子字符串。
  • 我用过 /img/project-gallery/'.substr($images, 1, -1)。'但它说警告:substr() 期望参数 1 是字符串,数组在中给出
  • 任何想法为什么我得到错误

标签: php html image


【解决方案1】:

以这种方式使用它对我有用

$images = explode(',',$result['images']);

然后在img src文件中

<img src="xxxx/xxx/<?php echo strtr($images[0],'[]',''); ?>">

只要您的图像在您的数据库中排列在一列中,它将只选择一个图像文件,例如

[image1.jpg]、[image2.jpg]、[image3.jpg] 等...

【讨论】:

    【解决方案2】:
    $temp = explode(',',$result['images'] );
    
    foreach($temp as $image){
        $images[]="/img/project-gallery/".trim( str_replace( array('[',']') ,"" ,$image ) );
    }
    //now your array of images cotaines to full source to each image
    
    foreach($images as $image){
      echo "<img src='{$image}' />";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 2013-12-24
      • 1970-01-01
      • 2018-04-20
      • 2012-10-24
      • 2016-11-17
      相关资源
      最近更新 更多