开发者

Split a string at ";" with php

I have a string called $gall开发者_如何学运维ery, $gallery is a list of image URLS The image urls are seperated by a semi- colon ;. Example

http://www.website.com/image1.jpg;http://www.website.com/image2.jpg;http://www.website.com/image3.jpg

How can I split this up and place each url in an image tag, I suppose using preg_split?

Thanks


You don't need preg_split for this.

$urls = explode(';', $string);
foreach ($urls as $url) {
    echo '<img src="'.$url.'" />';
}


No need for regular expressions with preg_split(), just a simple regex-less explode() will do since they're delimited by a semicolon.

foreach (explode(';', $gallery) as $url) {
    echo '<img src="' . htmlspecialchars($url, ENT_QUOTES) . '" alt="" />';
}


You can use str_replace

'<img src="' . str_replace(';','" /><img src="',$gallery) . '" />';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜