简体   繁体   中英

String replace PHP from a TEXT field within MySQL

I have the following in my database which stores as JSON and are images that I want to then display but I'm not sure on how to do a string replace to get rid of the [ " , and spaces and just grab the random string.png

["4a21da2670ce6528b2cffebf6f42cb1b8ade3c13.png","4d9465c0694079296b24f6e3be7b226eaa9f94dd.png"]

Above is what I get from my DB so how would one string replace this.

I have tried:

<img src="/uploads/products/'. str_replace("[", "", $pieces[$i]).'" width="35">

But to no avail.

Thanks in advance.

Try

$json = '["4a21da2670ce6528b2cffebf6f42cb1b8ade3c13.png","4d9465c0694079296b24f6e3be7b226eaa9f94dd.png"]';
$array = json_decode($json);

foreach($array as $a) {
    echo '<img src="/uploads/products/'.$a.'" width="35">';
}

This will output

<img src="/uploads/products/4a21da2670ce6528b2cffebf6f42cb1b8ade3c13.png" width="35">
<img src="/uploads/products/4d9465c0694079296b24f6e3be7b226eaa9f94dd.png" width="35">

( See example )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM