1

我有一个表格,用户可以使用 bbcode 引用其他人。当有人按下报价按钮时,textarea 值为:

[quote user=User date=1348246887 post=301]
User post text
[/quote]

现在,我转换为块的代码是:

$post = preg_replace("/\[quote user=(.*) date=(.*) post=(.*)](.*)\[\/quote\]/Uis", "<div class=\"quote\"><p>Quote by \\1 at time : \\2<a href=\"index.php?subject=".$_GET['subiect']."&post=\\3\">&nbsp;</a></p><span>\\4</span></div>", $post);

如何将日期转换为 preg_replace ?在 preg_replace 我不能这样做,因为 \2 的值没有设置。

4

1 回答 1

2

尝试这样的事情(我在链接中添加了“测试”,所以你可以看到链接 - 不确定你想要什么,但不间断的空格不会使链接可见。)我用于htmlentities安全以防万一“subiect” $_GET 变量(也许你的意思是“主题”?)包含标记或引号。当然,您可以根据需要自定义 date() 字符串的第一个参数。最后,我添加\s+了允许更灵活的空白。我还将分隔符“/”更改为“@”,因此您无需在正则表达式中转义“/”。

更新了旧的 PHP 兼容性:

<?php

$post = <<<HERE
[quote user=User date=1348246887 post=301]
User post text
[/quote]
HERE;
// ]  (just adding this comment to fix SO syntax colorer)

function replacer ($matches) {
    return '<div class="quote"><p>Quote by '.$matches[1].' at time : '.
        date('Y m d', $matches[2]).'<a href="index.php?subject='.
        htmlentities($_GET['subiect'], ENT_COMPAT, 'UTF-8').
        '&post='.$matches[3].'">test&nbsp;</a></p><span>'.
        $matches[4].'</span></div>';
}

$post = preg_replace_callback(
    '@\[quote\s+user=(.*)\s+date=(.*)\s+post=(.*)](.*)\[/quote\]@Uis',
    'replacer',
    $post
);

var_dump($post);

?>
于 2012-09-22T00:10:39.123 回答