简体   繁体   English

从MySQL中的TEXT字段字符串替换PHP

[英]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 我的数据库中存储了以下内容,这些内容存储为JSON,是要显示的图像,但是我不确定如何执行字符串替换以消除[“和空格,而只是获取随机字符串。 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 ) 请参见示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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