Php - 用两个分隔符爆炸一个字符串

时间:2014-07-08 16:10:17

标签: php explode

$string = 'folder1:image1.jpg|folder2:image2.jpg|folder3:image3.jpg';

我需要爆炸字符串才能得到以下结果:

<img src="/folder1/image1.jpg" />
<img src="/folder2/image2.jpg" />
<img src="/folder3/image3.jpg" />

一个分隔符不是问题,但我有两个分隔符,并且不知道该怎么做:

$imagedata = explode("|", $string);
foreach($imagedata as $image) {
echo "$image<br />";
}

4 个答案:

答案 0 :(得分:5)

您不需要两个分隔符,只需str_replace之后explode即可{/ 1}}:

$imagedata = explode("|", $string);
foreach($imagedata as $image) {
    echo "<img src='/".str_replace(":","/",$image) . "'/>";
}

答案 1 :(得分:1)

使用它:

$string = 'folder1:image1.jpg|folder2:image2.jpg|folder3:image3.jpg';
$imagedata = explode("|", $string);
foreach($imagedata as $image) {
    $img=explode(":", $image);
    echo '<img src="' . $img[0] . '/' . $img[1] . '" /><br />';
}

答案 2 :(得分:0)

也许你可以尝试2个foreach ...

$string = 'folder1:image1.jpg|folder2:image2.jpg|folder3:image3.jpg';

$imagedata = explode("|", $string);
foreach($imagedata as $folder) {
    $imageFolders = explode(":", $folder);
    foreach ($imageFolders as $image) {
        echo "$image<br />";
    }
}

然后可能只需将其保存在其他变量上......

答案 3 :(得分:0)

如果你想保持简短(但不清楚):

 $imagedata = '<img src="/' . implode("\" />\n<img src=\"", str_replace(':', '/', explode("|", $string))) . '" />';