Forum Moderators: coopster

Message Too Old, No Replies

implementing BBCODE?

can anyone advise me how i can implement BBCODE?

         

bysonary

8:52 pm on Nov 19, 2006 (gmt 0)

10+ Year Member



Hello,

I have been working on a system to add news and articles to a site, I have the core adding and removing out of the way and have had some very good advice from eelixduppy, the code for what i currently have is at the end of this post.

What i would to acheive is something where if i typed:

HELLO and wanted to make it bold i could wrap HELLO in something like the BBCODE on this forum. for example square brackets with a B inside to start the Bold area and square brackets with a /B inside to close the bold area, I would like to know how i can underline things with this too and perhaps add things such as font colours etc. a basic intro into how to get it to work would be great, here is the code i currently have if it helps in anyway at all.


<?php
include 'dbconnect.php';
$title = $_POST['title'];
$news = $_POST['news'];

$pattern = "/(http:\/\/[\w\.]+)/";
$replace = "<a href='$1'>$1</a>";

$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);

$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));
$date = date("D d M Y, g:i a");

$sqlquery = "INSERT INTO tnews (Title, Body, Date) VALUES ('$title','$news','$date')";

header("Location: display.php");

//print $sqlquery;
$results = mysql_query($sqlquery);

mysql_close($dbc);
?>

any help would be greatly appreciated. Thanks

Chris

mcibor

10:05 pm on Nov 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Bysonary and welcome to webMasterWorld!

I use:

function bb2html($text)
{
$trans = array("" => "<b>", "" => "</b>", "[u]" => "<u>", "[/u]" => "</u>",
"" => "<i>", "" => "</i>",
":-)" => "<img src=\"smile.gif\" title=\"smile :-)\">"
"
" => "<table><tr><td>", "
" => "</td></tr></table>"
);

$translated = strtr($text, $trans);
return $translated;
}

Hope this gives you some idea.

Michal

bysonary

10:19 pm on Nov 19, 2006 (gmt 0)

10+ Year Member



haha

I kind of understand that but the formatting of this BBCODE on this forum has knocked it out, could you at all tell me how i could go about implementing it in my code, such as placement?

sorry bit of a newbie to php not sure about functions etc.

mcibor

9:50 pm on Nov 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


Put the function anywhere (top of the page, bottom or in include)
then

<?php
include 'dbconnect.php';

function bb2html($text)
{
$trans = array("[b]" => "<b>", "[/b]" => "</b>",
"[u]" => "<u>", "[/u]" => "</u>",
"[i]" => "<i>", "[/i]" => "</i>",
":-)" => "<img src=\"smile.gif\" title=\"smile :-)\">"
"[quote]" => "<table><tr><td>", "[/quote]" => "</td></tr></table>"
);

$translated = strtr($text, $trans);
return $translated;

}

...
$title = $_POST['title'];
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);

$title = bb2html($title);
$news = bb2html($news);

$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));
...
?>

Sorry, but in the previous message I forgot to unset bbcode translation on this forum
Is this sufficient?

Regards
Michal

bysonary

3:20 pm on Nov 27, 2006 (gmt 0)

10+ Year Member



Thats great.

but how could i go about doing things like font colours and URLs?

mcibor

10:50 pm on Nov 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Bysonary!

I would download some forum, eg phpBB and take the bbcode function.

Regards
Michal

[edited by: coopster at 6:44 pm (utc) on Nov. 30, 2006]
[edit reason] removed link [/edit]

henry0

12:14 pm on Nov 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Michal,
Very interesting link
Too bad it does not exist in EN of FR :)

Sounds like an interesting combo CSS/PHP
If you do it in EN, then I'll try to make it in FR :)

Henry

stephen03

12:45 am on Dec 5, 2006 (gmt 0)

10+ Year Member



see [en.wikipedia.org...]

Stuperfied

12:35 am on Dec 7, 2006 (gmt 0)

10+ Year Member



This is kina what I use, it needs to be upgraded with mysql_real_escape_string() if you wish to use it for MySQL. Its probably not as easy to use as some of the ones youve seen here but it works and if anything you might get some good regular expressions from it. You will find references to functions I havent shown here but just ignore them because they are not important.

// write new content to file
if ($post_content = $_POST['textarea']) {
$new_content = trim($post_content);
$new_content = htmlentities($new_content, ENT_NOQUOTES); // must dissable before converting bbcode to html
$new_content = replaceTags($new_content); // now our code works and the users does not
putFile($filename, $new_content);
}

// display content
$lines = getFile($filename);
$content = getEditable($lines);
$content = html_entity_decode($content); // must enable html to swap tags
$content = swapTags($content);
$content = htmlentities($content, ENT_NOQUOTES); // dissable html again
insertForm($content, $linkSet, $editPage);
}

// get editable content
function getEditable($lines) {
// Loop through array
foreach ($lines as $key => $line) {
if (preg_match('/<!\-\- TemplateBeginEditable name="Content" \-\->/', $line)) {
$editable = "true";
continue;
}
if (preg_match('/<!\-\- TemplateEndEditable \-\->/', $line)) {
$editable = "false";
}
if ($editable == "true") {
$content .= $line;
}
}
return $content;
}

function replaceTags($content) {
$patterns[0] = '/\[b\]/';
$patterns[1] = '/\[\/b\]/';
$patterns[2] = '/\[u\]/';
$patterns[3] = '/\[\/u\]/';
$patterns[4] = '/\[i\]/';
$patterns[5] = '/\[\/i\]/';
$patterns[6] = '/\[color\=("¦\')?(\w+)("¦\')?\]/';
$patterns[7] = '/\[size\=("¦\')?(\d+)("¦\')?\]/';
$patterns[8] = '/\[\/color\]/';
$patterns[9] = '/\[\/size\]/';
$patterns[10] = Chr(13).Chr(10);
$patterns[11] = '/\r?\n¦\r/';
$patterns[12] = '/\[url=("¦\')?(http[s]?:\/\/[^\]]+)("¦\')?\]([^\[]+)\[\/url\]/';

$replacements[0] = '<b>';
$replacements[1] = '</b>';
$replacements[2] = '<u>';
$replacements[3] = '</u>';
$replacements[4] = '<i>';
$replacements[5] = '</i>';
$replacements[6] = '<font color=$1$2$3>';
$replacements[7] = '<font size=$1$2$3>';
$replacements[8] = '</font><!-- /color -->';
$replacements[9] = '</font><!-- /size -->';
$replacements[10] = '<br />';
$replacements[11] = '<br />';
$replacements[12] = '<a href=$1$2$3>$4</a>';

ksort($patterns);
ksort($replacements);
$content = preg_replace($patterns, $replacements, $content);

// this must come after preg_replace to preserve the newline characters
$content = '<!-- TemplateBeginEditable name="Content" -->' . Chr(13) . Chr(10) . $content . Chr(13) . Chr(10) . '<!-- TemplateEndEditable -->';
return $content;
}

function swapTags($content) {
$patterns[0] = '/<b>/';
$patterns[1] = '/<\/b>/';
$patterns[2] = '/<u>/';
$patterns[3] = '/<\/u>/';
$patterns[4] = '/<i>/';
$patterns[5] = '/<\/i>/';
$patterns[6] = '/<font color=("¦\')?(\w+)("¦\')?>/';
$patterns[7] = '/<font size=("¦\')?(\d+)("¦\')?>/';
$patterns[8] = '/<\/font><!\-\- \/color \-\->/';
$patterns[9] = '/<\/font><!\-\- \/size \-\->/';
$patterns[10] = '/<br \/>/';
$patterns[11] = '/<a href=("¦\')?(http[s]?:\/\/.*)("¦\')?>(.+)<\/a>/';

$replacements[0] = '[b]';
$replacements[1] = '[/b]';
$replacements[2] = '[u]';
$replacements[3] = '[/u]';
$replacements[4] = '[i]';
$replacements[5] = '[/i]';
$replacements[6] = '[color=$1$2$3]';
$replacements[7] = '[size=$1$2$3]';
$replacements[8] = '[/color]';
$replacements[9] = '[/size]';
$replacements[10] = Chr(13).Chr(10);
$replacements[11] = '[url=$1$2$3]$4[/url]';

ksort($patterns);
ksort($replacements);
$content = preg_replace($patterns, $replacements, $content);

return $content;
}

[edited by: Stuperfied at 12:36 am (utc) on Dec. 7, 2006]