BBcode parsing problem (pulling my hair out rite now!)
Posted: Wed Jun 09, 2010 10:15 pm
Hello,nice to meet you all. i am new to the forum and i need a little help.
i have been wrestling with this code all evening
desperately trying to have a lightweight bbcode parsing on my tutorials webpage. i for the life of me cannot figure out what the problem is.
(btw i did not write this code, i found it in a tutorial page ironically)
Here is the main bbcode.php
and here is the code that i am using to call this class and display it
however i am still having the same problem of it not parsing half of the BBcodes, im not sure if i havent figured out the correct BBcode syntax... but even half the guys examples didnt work.
any help would be massively appreciated.
i have been wrestling with this code all evening
(btw i did not write this code, i found it in a tutorial page ironically)
Here is the main bbcode.php
Code: Select all
<?php
// Filename: bbcode.php
// Version: 0.1
//
class bbcode {
function code_box($text)
{
$output = "<div class=\"code\"><h6>Code</h6>\\1</div>";
return $output;
}
function quote($text)
{
$output = "<blockquote><h6>Quote:</h6>\\1</blockquote>";
return $output;
}
function htmlout($text)
{
$text = stripslashes($text);
$text = htmlspecialchars($text);
$text = nl2br($text);
return $text;
}
function parse($text)
{
// First: If there isn't a "[" and a "]" in the message, don't bother.
$text = " " . $text;
if (! (strpos($text, "[") && strpos($text, "]")) )
{
return $text;
}
else
{
// de uiteindelijke code
$text = $this->htmlout($text);
$text = preg_replace("/\\[b\\](.+?)\[\/b\]
/is",'<b>\1</b>', $text);
$text = preg_replace("/\\[i\\](.+?)\[\/i\]
/is",'<i>\1</i>', $text);
$text = preg_replace("/\\[u\\](.+?)\[\/u\]
/is",'<u>\1</u>', $text);
$text = preg_replace("/\[s\](.+?)\[\/s\]/is",'
<s>\1</s>', $text);
$text = preg_replace("/\[code\](.+?)\[\/code\]/is"
,"".$this->code_box('\\1')."", $text);
$text = preg_replace("/\[quote\](.+?)\[\/quote\]/is"
,"".$this->quote('\\1')."", $text);
$text = eregi_replace("\\[img]([^\\[]*)\\[
/img\\]","<img src=\"\\1\">",$text);
$text = eregi_replace("\\[size([^\\[]*)\\](
[^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text);
$text = eregi_replace("\\[color=([^\\[]*)\\]
([^\\[]*)\\[/color\\]","<font color=\"\\1\">\\2</font>",$text);
return $text;
}
}
}
?>Code: Select all
function gettutorial($getid){
$query = mysql_query("SELECT * from tutorials WHERE id ='".$getid."'") or die(mysql_error());
$row = mysql_fetch_array($query);
echo "<b><u> ".$row['title']."</b></u> | Posted By: <b>".$row['creator']."</b> on ".$row['time']."<br>".$row['description']."<br>Category:<b>".$row['category']."</b><br>";
$bbcode = new bbcode;
$my_content = $row['content'];
echo $bbcode->parse($my_content);
}
any help would be massively appreciated.