2013-05-22 26 views
0
$tid = $_GET['tid']; 
require_once $_SERVER['DOCUMENT_ROOT'] . '/common/class/class.thread.php'; 
$thread = new getThread; 
$getThread = $thread->get_content($tid); 
require_once $_SERVER['DOCUMENT_ROOT'] . '/common/class/class.thread.php'; 
$thread = new getThread; 
$getThread = $thread->get_content($tid); 
$attachmentTable = substr($tid, -1); 
$tdatabase = new threadDatabase(); 
$tdatabase->query('SELECT * FROM mycms_forum_attachment_' . $attachmentTable . ' WHERE tid = :tid ORDER BY aid DESC'); 
$tdatabase->bind(':tid', $tid); 
$attachmentContainer = $tdatabase->resultset(); 
print_r($attachmentContainer); 

//following is content , it pull from db($getThread) 
//main content start 
[attach]42[/attach] 


[align=left][attach]53[/attach][/align] 
content bla bla bla      content bla bla bla 
[align=left][attach]52[/attach][/align] 
content bla bla bla 


content bla bla bla 
//main content end 

//following is attachment file 
[0] => Array ([aid] => 53 [tid] => 32 [pid] => 32 [uid] => 1 [filesize] => 152633 [attachment] => 201305/22/142619h42az34077hhra0p.jpg [width] => 1080) 
[1] => Array ([aid] => 52 [tid] => 32 [pid] => 32 [uid] => 1 [filesize] => 89015 [attachment] => 201305/22/142618wx3njhgf3n883zxr.jpg [width] => 1080) 

如何使用的preg_replace更改[附] 53 [/附加]是discuz! X 3.0的bbcode和附上的preg_replace

在src取從陣列,其中助劑= 53,由於它是[附] 53 [/ attach]和寬度太

我已經嘗試以下,但得到錯誤,我知道這樣做是錯誤的,因爲$ attachmentContainer [$ 1] ['attachment']以0開頭,53是不存在,陷入困境,不想做。我的問題: 在JavaScript或PHP更好嗎? 我如何preg_replace所有[attach] [/ attach]與正確的src和寬度存儲在db? 並檢查內容,我如何處理這些「空間」到
換行符?

我試圖拉扯discuz!直接從數據庫x3.0線程內容。

感謝教學!

回答

0

我不太明白你想要什麼,但是從你最後一塊代碼我會說你需要preg_replace_callback,因爲在調用preg_replace之前$attachmentContainer[$1]['attachment']不應該評估。

所以,你的代碼的最後一塊應該是這樣的:

$threadContainer = preg_replace_callback(
    '/\[attach\](.*?)\[\/attach\]/i', 
    function ($matches) { 
     return '<img src="' . $attachmentContainer[$matches[1]]['attachment'] . '" width="' . $attachment[$matches[1]]['width'] . '" />'; 
    }, 
    $getThread['message'] 
); 
+0

嗯...是$匹配[1]將匹配的援助? –

+0

是的。我給了你鏈接到文檔的鏈接,所以你可以親自看看它是如何工作的。 –

+0

謝謝,會讀它 –