MyBB Community Forums

Full Version: BBCode Parsing problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've gotten some bbcode parsing code from somewhere in here. What it's missing is quote and list bbcode parsing.
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>General Emulation</title>
</head>

<body>

<p align="center"><b><font size="6">General Emulation</font><font size="6"> News</font></b></p>

<?php
$forumpath = "./forums/"; // Path to your forums root directory (with trailing slash)
$fid = "43"; // The forum ID to pull the annoucements from
$limit = "10"; // The amount of annoucements to show
require $forumpath."inc/config.php";

function bb_code(&$string){
    $pattern = array(
        //[ul]
        '#\[list\](.*?)\[\/list\]#si',
        //[*]
        '#\[\*\](.*?)(\r\n|\r|\n|<br>|<br />|<br/>)#si',
        //[url]
        '#\[url\](.*?)\[\/(url)\]#si',
        //[url=XXX]
        '#\[url=(&quot;|"|\'|)(.*?)\\1\](.*?)\[\/url\]#si',
        //[i],[u]
        '#\[u\](.*?)\[/u\]#si',
        '#\[i\](.*?)\[/i\]#si',
        //[b]
        '#\[b\](.*?)\[/b\]#si',
        //[img]
        '#\[img\](.*?)\[\/img\]#si',
        // font size
        '#\[size=(&quot;|"|\'|)([0-9\+\-]+)\\1\](.*?)\[\/size\]#si',        '#(\r|\n|\r\n)#si');

    $replacement = array(
        //ul
        "\t\t<ul>$1</ul>\r\n",
        //li
        "\t\t\t<li>$1</li>\r\n",
        //a
        '<a href="$1">$1</a>',
        //a
        '<a href="$2">$3</a>',
        //i, u
        '<u>$1</u>',
        '<em>$1</em>',
        //b
        '<strong>$1</strong>',
        //img
        '<img src="$1" alt="User Posted Image" />',
        //size
        '<font size="\2">\3</font>',        "<br />");
    $string = preg_replace ($pattern, $replacement, stripslashes($string) );    $string = str_replace ('<br /><br />', "</p>\r\n\t\t<p>", $string);
}
mysql_select_db($config['database'], mysql_connect($config['hostname'], $config['username'], $config['password']));

$query = mysql_query("SELECT t.tid,t.subject,t.uid,t.username,t.dateline,t.replies,p.message,u.avatar FROM ".$config['table_prefix']."threads t LEFT JOIN ".$config['table_prefix']."posts p ON (t.tid = p.tid) LEFT JOIN ".$config['table_prefix']."users u ON (t.uid = u.uid) WHERE t.fid='".$fid."' AND t.visible='1' AND t.closed NOT LIKE 'moved|%' AND p.replyto='0' ORDER BY t.tid DESC LIMIT 0, ".$limit."") or die(mysql_error());
while ($news = mysql_fetch_array ($query)){
    $date = gmdate ("jS F Y", $news['dateline']);
    $time = gmdate ("h:i A", $news['dateline']);

    //Parse BBCode
    bb_code($news['message']);

    if($news['replies'] == "1"){
        $replytext = "reply";
    } else {
        $replytext = "replies";
    }

    if($news['avatar'] != ""){
        if(substr($news['avatar'], 0, 7) != "http://")
        {
            $url = $forumpath;
        }
        $avatarbit = "<img style='float:left; margin:10px;'src=\"".$url.$news['avatar']."\" alt=\"".$url.$news['avatar']."\" />\r\n\t\t";
    } else {
        $avatarbit = "";
    }

    echo "\t<div class=\"news\">\r\n\t\t";
    echo "<div class='details'>(<a href=\"".$forumpath."showthread.php?tid=".$news['tid']."\">".$news['replies']." ".$replytext." </a>) | Posted by <a href=\"".$forumpath."member.php?action=profile&amp;uid=".$news['uid']."\">".$news['username']."</a> on ".$date." at ".$time."</div>\r\n\t\t";
    echo "<h2>".$news['subject']."</h2>\r\n\t\t";
    echo $avatarbit."<p>".$news['message']."</p>\r\n\t\t<div style=\"clear:both;\"></div>\r\n\t";
    echo "</div>\r\n";
}
?>
</body>

</html>
Now, what I attempted was to import the quote parsing found in forums/inc/functions_post.php. Specifically, this section:
$pattern = array("#\[quote=(?:&quot;|\"|')?(.*?)[\"']?(?:&quot;|\"|')?\](.*?)\[\/quote\]#si",
					 "#\[quote\](.*?)\[\/quote\]#si");

	$replace = array("<div class=\"quote_header\">$1 $lang->wrote</div><div class=\"quote_body\">$2</div>",
					 "<div class=\"quote_header\">$lang->quote</div><div class=\"quote_body\">$1</div>\n");

	while (preg_match($pattern[0], $message) or preg_match($pattern[1], $message))
	{
		$message = preg_replace($pattern, $replace, $message);
	}
	$message = str_replace("<div class=\"quote_body\"><br />", "<div class=\"quote_body\">", $message);
	$message = str_replace("<br /></div>", "</div>", $message);
	// Remove [attachment=x] from quoted posts.
    	//$message = preg_replace("#\[attachment=([0-9]+?)\]#i", "", $message);
	return $message;
My question is, how would I import the $pattern and $replacement into my the existing code so that quoted news posts show the quote box, rather than the quote tags? I've been trying for hours to figure this out, and all I keep doing is killing the page.Toungue Any help would be GREATLY appreciated. Thanks in advance.
Hmm, is this just not possible to do? Any guidance (if it is) would be greatly appreciated.
Yeah, that's where I got the code from, but it lacks parsing for bbocode with list and quote, not to mention that smilies don't parse either.
Edit: OK, didn't see that quote tags were added. Now it's just a matter of figuring out how to get a border around it and smilies, but I guess you're already trying to sort that.Toungue Thanks for the help.Smile