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 将 [attach]53[/attach] 更改为

src 从数组中获取aid=53,因为它也是 [attach]53[/attach] 和宽度

我尝试了以下方法,但出现错误,我知道这样做是错误的,因为 $attachmentContainer[$1]['attachment'] 以 0 开头,而 53 不存在,卡住了,不知道该怎么做。

$threadContainer = preg_replace('/\[attach\](.*?)\[\/attach\]/i', '<img src="' . $attachmentContainer[$1]['attachment'] . '" width="' . $attachment[$1]['width'] . '" />', $getThread['message']);

我的问题:用 javascript 或 php 做得更好?我如何在 db 中使用正确的 src 和宽度存储 preg_replace 所有 [attach][/attach] ?并检查内容,我如何处理这些“空格”来
换行?

我正在尝试拉 discuz!x3.0 线程内容直接来自 db。

感谢您的教导!

4

1 回答 1

0

我不太明白你想要什么,但是从你的最后一段代码中我会说你需要preg_replace_callback,因为在调用$attachmentContainer[$1]['attachment']之前不应该评估。preg_replace

因此,您的最后一段代码应如下所示:

$threadContainer = preg_replace_callback(
    '/\[attach\](.*?)\[\/attach\]/i',
    function ($matches) {
        return '<img src="' . $attachmentContainer[$matches[1]]['attachment'] . '" width="' . $attachment[$matches[1]]['width'] . '" />';
    },
    $getThread['message']
);
于 2013-05-22T15:14:31.913 回答