2011-11-01 68 views
0

我需要一些幫助,爲我的文章中的所有圖像生成圖像。 目前得到的只有我用這個第一個圖像:創建矢量並獲取所有圖像?

$first_img = ''; 
$mycontent = $row['post_content']; 
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
$first_img = $matches [1] [0]; 
if(empty($first_img)){ //Defines a default image 
$first_img = "/img/default.png";} 

這讓只從文章的第一形象..

所以我需要它,讓所有從文章的圖片,並連續顯示它們。

謝謝

回答

2

preg_match_all函數將返回匹配結果的數組,foreach循環就足夠了。

http://php.net/manual/en/function.preg-match-all.php

foreach($matches as $val) { 
    echo $val; 
} 

更好的模式:/<img(?:(?!src).)+src="?([^"\']+)/i

要處理多個匹配,你可以做到以下幾點:

$mycontent = '<img something="null" src="ggggg.gif"><br/><img src="bob.jpg">'; 
$output = preg_match_all('/<img(?:(?!src).)+src="?([^"\']+)/i', $mycontent, $matches, PREG_SET_ORDER); // find all src attributes 
foreach($matches as $val) { //loop over <img> tags matches 
    echo $val[1]; 
} 
+0

請您可以將代碼粘貼在這裏應該如何?謝謝 – JoinOG

+0

更新了答案,改進了模式。 –