2010-10-31 45 views
0

我們有一些bbcode,我試圖設置某個代碼的能力來調用從我們的照片庫加載一些圖像的函數。所以如果有人放入相冊號碼,它會將最近的5張照片取出。當我們定期調用這個函數時(在另一頁上,就像常規的php語句一樣),它按預期工作。但是當我們通過preg_replace系統調用這個函數時,它會在頁面頂部加載這個函數。從preg_replace調用php函數會導致函數在頁面頂部加載

請點擊這裏:http://www.greendayauthority.com/news/1961/ 網站頂部的南瓜圖片應該位於新聞帖子的下方,「感謝Marcus M.發送新聞」。 - 這是我放在BB代碼的[picvault] 1 [/ picvault]

下面是我們如何爲設置高亮

$newsPost = preg_replace(array_keys($bbcode), array_values($bbcode), $newsPost); 

這裏變出的值是調用我們的功能$設置高亮的部分

"/\[picvault\](.*?)\[\/picvault\]/e" => "relatedImages($1)", 

而這裏的relatedImage功能

function relatedImages($albumID) { 

    $queryImage = mysql_query("SELECT * FROM cpg140_pictures WHERE aid = $albumID ORDER BY pid DESC LIMIT 5"); 

    echo "<div class='relatedImages'>"; 
    while($images = mysql_fetch_array($queryImage)) { 

      $relImgPath = $images['filepath']; 
      $relImgName = $images['filename']; 
      $relThumbImgUrl = "http://www.greendayauthority.com/Picture_Vault/albums/$relImgPath/thumb_$relImgName"; 
      $relFullImgUrl = "http://www.greendayauthority.com/Picture_Vault/albums/$relImgPath/$relImgName"; 

     echo "<div class='relImage'> 
         <a href='$relFullImgUrl' rel='lightbox-$albumID'><img src='$relThumbImgUrl'></a> 
        </div>"; 
    } 

    echo " <div class='relImage'> 
         <a href='http://www.greendayauthority.com/Picture_Vault/thumbnails.php?album=$albumID' target='_top'><img src='http://www.greendayauthority.com/images/viewmorephotos.png'></a> 
       </div>"; 
    echo "</div>"; 
} 

回答

2

如果您使用的功能callbac k它會輸出其HTML片段馬上。發生這種情況是因爲它使用了echo。並且,當您的preg_replace在真正的頁面輸出開始之前調用echo時,圖像html將在頁面的其餘部分之前。

解決方法:使相關圖像回調函數使用return而不是回顯。