を分割:複数の文字列は、複数のイメージを持つ私のデータベースには1つの文字列、すべてのソートに複数回
url/image-1.jpg|text 1#
url/image-2.jpg|text 2#
url/image-3.jpg|text 3#
だけでなく、私は、alt属性を持つ各画像新しいimgタグのために必要です。ここでは、コードです:
<img src="url/image-1.jpg" alt="text 1">
<img src="url/image-2.jpg" alt="text 2">
<img src="url/image-3.jpg" alt="text 3">
私が爆発すると、ループのためにしようとしましたが、それはない作品。うまく
<?php
// array of urls
$urls = [];
// add 3 urls to the array
array_push($urls, "url/image-1.jpg|text 1#");
array_push($urls, "url/image-2.jpg|text 2#");
array_push($urls, "url/image-3.jpg|text 3#");
// array that contains the extracted values from urls
$matches = [];
// loop through all urls
foreach($urls as $url){
$match = null;
// try to match the pattern
preg_match("/\|(.*)#/", $url, $match);
// if it does match the pattern then add it to the matches array
if($match){
array_push($matches, $match[1]);
}
}
// print all matches
// outputs:
// text 1
// text 2
// text 3
foreach($matches as $match){
echo $match . "<br>";
}
?>