Parsing Xenforo's BB Code on an External Page.

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.

PHP:
$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

PHP:
function bb($content) {
  $search = array (
    '#\[CENTER\](.+)\[\/CENTER\]#iUs',
    '#\[IMG\](.+)\[\/IMG\]#iUs',
    '#\[B\](.+)\[\/B\]#iUs',
    '#\[COLOR=(.+)\](.+)\[\/COLOR\]#iUs',
    '#\[SIZE=(.+)\](.+)\[\/SIZE\]#iUs',
    '#\[URL\](.+)\[\/URL\]#iUs',
    '#\[URL=(.+)\](.+)\[\/URL\]#iUs',
    '#\[FONT=(.+)\](.+)\[\/FONT\]#is',
    '#\[EMAIL=(.+)\](.+)\[\/EMAIL\]#iUs',
  );
  $replace = array (
    '<span style="text-align: center;">$1</span>',
    '<img style="max-width: 95%;" src="$1">',
    '<b>$1</b>',
    '<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);
  return $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.
 
I have managed to get it parsing using

Code:
    $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.
 
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.

PHP:
<?php

$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 DESC LIMIT 4";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $formatter = XenForo_BbCode_Formatter_Base::create();
    $parser = new XenForo_BbCode_Parser($formatter);
    $news = $parser->render($row['message']);
    echo '<div class="news_titlebar">';
    echo '<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;
    echo "</div>";
    echo '<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'];
    echo " Comments". '</a></span></div>';
}
?>
 
Put this right after the opening PHP tag:
PHP:
header('Content-Type: text/html; charset=utf-8');
 
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.

PHP:
<?php

$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 DESC LIMIT 4";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $formatter = XenForo_BbCode_Formatter_Base::create();
    $parser = new XenForo_BbCode_Parser($formatter);
    $news = $parser->render($row['message']);
    echo '<div class="news_titlebar">';
    echo '<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;
    echo "</div>";
    echo '<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'];
    echo " Comments". '</a></span></div>';
}
?>
Your connection to your dbase needs to be UTF8 also.
 
When you set the connection you can specify UTF-8

If using PDO the preferred method
PHP:
$this->options = array(PDO::ATTR_EMULATE_PREPARES => FALSE,
                            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
            );

parent::__construct($dsn, $arrayInfo['username'], $arrayInfo['password'], $this->options);

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

$this->db->set_charset("utf8");
 
Top Bottom