0

I have a database with a column named images

it looks like:

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

how can i extract this information and display each image using PHP like:

$sql="SELECT * from table ";
$rs=mysql_query($sql,$conn);
$result=mysql_fetch_array($rs);

echo '<img src="image1.jpg" />';
echo '<img src="image2.jpg" />';
echo '<img src="image3.jpg" />';
user2710234
  • 3,177
  • 13
  • 36
  • 54

3 Answers3

5
foreach(explode(',',$result['column']) as $image){
    echo '<img src='.strtr($image,'[]','""').' />';
}
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
0

The easiest way will be regexp.

preg_match_all('/\[(.*?)\]/', $field, $images);
$images = $images[1];

Now $images will contain array of all found images.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
  • Probably not easier, and certainly too heavyweight for the requirement. –  Oct 10 '13 at 21:01
  • 1
    @MikeW You can test it but I don't think it'll be much slower than other solutions in that case. If of course any difference will be noticeable. – Elon Than Oct 10 '13 at 21:05
0

You should use str_replace() & explode() functions to deal with this situation and avoid using regex:

<?php

$data = "[image1.jpg],[image2.jpg],[image3.jpg]";

$data = str_replace("]","", str_replace("[", "", $data));

$exploded = explode("," $data);

?>

The array will contain the images each in an element without the "[" and "]", now all you have to do is loop throught it and echo it.

root
  • 59
  • 3