2017-03-21 3 views
1

in einer Zeichenkette in der Datenbank mit mehreren Bildern, alle Art:Mehrere String aufgeteilt mehrmals

url/image-1.jpg|text 1# 
url/image-2.jpg|text 2# 
url/image-3.jpg|text 3# 

gut, ich brauche für jedes Bild neu img-Tag mit alt-Attribute. Hier ist Code:

<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"> 

Ich habe mit explode und for-Schleife versucht, aber es funktioniert nicht.

$images = " 
url/image-1.jpg|text 1# 
url/image-2.jpg|text 2# 
url/image-3.jpg|text 3#"; 

$parts = explode("#", $images); 
for($i = 0; $i < count($parts); $i++){ 
    $parts2[$i] = explode("|", $parts[$i]); 
    for($y=0; $y < count($parts2[$i]); $y++){   

     echo '<img src="'.trim($parts2[$y]).'" alt="$part[0]" />';    
    } 
} 

Antwort

0

Sie nutzen könnten explodieren zweimal

$str ="url/image-1.jpg|text 1# 
url/image-2.jpg|text 2# 
url/image-3.jpg|text 3#"; 

$str_row = explode('#',$str); 

for ($i=0; $i<count($str_row)-2; $i++){ 
    $parts = explode('|',$str_row[$i]); 
    echo '<img src="'.$parts[0] . " alt=" . $parts[1] .">"; 

} 
0

Ich möchte einige Code teilen

$string="url/image-1.jpg|text 1# 
    url/image-2.jpg|text 2# 
    url/image-3.jpg|text 3#"; 

$imgs=explode("#", $string); 

foreach ($imgs as &$img) { 
    $image=explode("|", $img); 
    echo "<img src='".$image[0]."' alt='".$image[1]."'>"; 
} 
0

Sie es mithilfe von regex wie diese lösen können:

<?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>"; 
} 

?> 
0

gut Ich habe mein Skript geändert und es gelöst. jetzt funktioniert es gut.

$parts = explode("#", $images);   
for($i = 0; $i < count($parts); $i++){ 
    $teile2 = explode("|", $teile[$i]);     
    for($y = 0; $y < count($teile2[0]); $y++){      
     echo '<img src="'.$urlAdresse.trim($urlImg).'" alt="'.trim($teile2[1]).'" />'."\n"; 

    } 
} 
Verwandte Themen