1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Parsing Xenforo's BB Code on an External Page.

Discussion in 'XenForo Development Discussions' started by Draex, Jul 6, 2013.

  1. Draex

    Draex Member

    I was just wondering if anyone knows of the simplest way to parse BB Code from Xenforo's post table in the correct fashion on an external page located on the same webserver.

    (Complete php novice here)

    Currently I am using the following code to query the relevant info.

    $query="SELECT xf_post.thread_id, xf_thread.title, xf_thread.reply_count, xf_thread.view_count, xf_post.username, xf_post.user_id, xf_post.post_date, xf_post.message FROM xf_thread, xf_post WHERE xf_thread.first_post_id = xf_post.post_id AND xf_thread.node_id = '10' ORDER BY xf_post.post_date LIMIT 4";

    $result mysql_query($query) or die(mysql_error());
    and then using

    function bb($content) {
    $search = array (
    $replace = array (
    '<span style="text-align: center;">$1</span>',
    '<img style="max-width: 95%;" src="$1">',
    '<span style="color: $1;">$2</span>',
    '<span style="font-size: $1;">$2</span>',
    '<a href="$1">$1</a>',
    '<a href="$1">$2</a>',
    '<span style="font-family: $1;">$2</span>',
    '<a href="mailto:$1">$2</a>'
    $newtext preg_replace($search$replace$content);
    $newtext nl2br($newtext);//second pass
    $newtext preg_replace('#<br />(\s*<br />)+#''<br />'$newtext);
    To parse it, and then displaying it with a while loop.

    The regex does not want to parse the nested BBcode elements, and I haven't found a way to call xenforo's parser to the external page yet, any help in this matter could be appreciated.

    This is the page, showing what is currently doing. For comparison here is the xenporta link.

    Thanks in advanced.
  2. Jeremy

    Jeremy Well-Known Member

  3. Draex

    Draex Member

    I have managed to get it parsing using

        $formatter = XenForo_BbCode_Formatter_Base::create();
        $parser = new XenForo_BbCode_Parser($formatter);
        $news = $parser->render($row['message']);
    But it appears to be leaving � symbols in place of of several different types of symbols. (can be seen here. ) Also wondering if its possible to extend this to parse attachments.
  4. Jeremy

    Jeremy Well-Known Member

    Are you modifying the content in any way before passing it to the parser?
  5. cclaerhout

    cclaerhout Well-Known Member

    The charset of your header (check the meta) is not correct. Use utf8.
  6. Draex

    Draex Member

    I have changed the charset to utf8 to no effect, and I am not modifying the content before parsing it, the whole block of code for that section is.


    ="SELECT xf_post.thread_id, xf_thread.title, xf_thread.reply_count, xf_thread.view_count, xf_post.username, xf_post.user_id, xf_post.post_date, xf_post.message FROM xf_thread, xf_post WHERE xf_thread.first_post_id = xf_post.post_id AND xf_thread.node_id = '10' ORDER BY xf_post.post_date DESC LIMIT 4";
    $result mysql_query($query) or die(mysql_error());
    $row mysql_fetch_array($result)){
    $formatter XenForo_BbCode_Formatter_Base::create();
    $parser = new XenForo_BbCode_Parser($formatter);
    $news $parser->render($row['message']);
    '<div class="news_titlebar">';
    '<a href="?page=thread&id='$row['thread_id']. '">'$row['title']. '</a>''<span style="Float: Right;">'date("M d Y h:i:s A"$row['post_date']). '</span>'"</div><br />"'<div class="news_content">'$news;
    '<div class="news_footer">'"Posted by: "'<a href="http://www.avarice.us/members/'$row['user_id']. '">'$row['username']. '</a>''<span style="Float: Right;">'$row['view_count']. " Views "'<a href="?page=thread&id='$row['thread_id']. '">'$row['reply_count'];
    " Comments"'</a></span></div>';
  7. Jeremy

    Jeremy Well-Known Member

    Put this right after the opening PHP tag:
    header('Content-Type: text/html; charset=utf-8');
    Draex likes this.
  8. EasyTarget

    EasyTarget Well-Known Member

    Your connection to your dbase needs to be UTF8 also.
    Draex likes this.
  9. Draex

    Draex Member

    Thanks, adding
    mysql_query("set names 'utf8'"); 
    before query fixed it.
  10. EasyTarget

    EasyTarget Well-Known Member

    When you set the connection you can specify UTF-8

    If using PDO the preferred method
    $this->options = array(PDO::ATTR_EMULATE_PREPARES => FALSE,

    parent::__construct($dsn$arrayInfo['username'], $arrayInfo['password'], $this->options);
    or if you are forced to use SQLi
    $this->db = new mysqli($arrayInfo['host'], $arrayInfo['username'], $arrayInfo['password'], $arrayInfo['dbname']);


Share This Page