PHP:如何用扩展数据(递归)替换bbcode-quote?

时间:2012-12-10 00:27:32

标签: php recursion replace bbcode

这是我引用帖子的bbcode(12345是原帖的ID):

[quote=12345] ... citation ... [/quote]

我想显示这样的引用:

<blockquote>
    <a href="">Username</a> at 2012-09-12 11:00 a.m. (<a href="">original</a>):
    ... citation ...
</blockquote>

想法(伪代码):

$bbcode_content = "[quote=12345] ... citation ... [/quote]";

// 1. get the $post_id
$post_id = replace('/\[quote\=(.*?)\]/is','$1', $bbcode_content);

// 2. select the post data from databse
$post_obj = $DB->select("SELECT `author`, `timestamp` FROM `posts` WHERE id=".$post_id);

// 3. build an extended bbcode tag
$bbcode_content = "[quote post_id={$post_id} timestamp={$post_obj->timestamp} author={$post_obj->author}]";

// 4. bbcode to html
$html = replace("[quote post_id=(*) timestamp=(*) author=(*)]", '<blockquote><a href="">$1</a> at '.date("Y-m-d h:i", $2).' (<a href="forum/post/$3">original</a>)', $bbcode_content);
$html = replace("[/quote]", "</blockquote>", $html);

问题

  1. 如何将这些东西替换为递归?也许用preg_replace_callback
  2. 我的第四点“bbcode to html”的正则表达式是什么?
  3. 我有点困惑。你会如何解决这个问题?

    提前致谢!

1 个答案:

答案 0 :(得分:1)

您始终可以使用bbcode extension in PHP为您处理此问题。