Page 1 of 1

[Script/Tutorial] BBCode

Posted: Mon Jan 11, 2010 1:16 pm
by Falken
This is more of a script that is free to use rather than a tutorial, but you might learn something from it too. I wrote this simple script for one of my projects. It is also very simple to add your own bbcode to this script.

Code: Select all

<?php
	// Convert a string with bbcode to html
	function bbToHtml($str)
	{
		$bbcode = array(
			'/\\[b\\]/i', '/\\[\/b\\]/i',
			'/\\[i\\]/i', '/\\[\/i\\]/i',
			'/\\[u\\]/i', '/\\[\/u\\]/i',
			'/\[url\=(.*?)\](.*?)\[\/url\]/is');
		
		$htmlcode = array(
			'<b>', '</b>',
			'<i>', '</i>',
			'<u>', '</u>',
			'<a href="$1">$2</a>');
		
		$str = preg_replace($bbcode, $htmlcode, $str);
		
		return $str;
	}
?>
Here is an example of how to use it:

Code: Select all

$myString = "[b][i][/i][/b]";
echo bbToHtml ($myString);
This would give the following result:
Hello World

Ofcourse you can change $myString to something you fetched from example a database :)

Re: [Script/Tutorial] BBCode

Posted: Mon Jan 11, 2010 2:53 pm
by hallsofvallhalla
this is very nice. Thanks Falk!

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 12:13 am
by Torniquet
n1 falk.

my question is... how would you go about doin quoting??

because with a quote, it becomes a div...

and if some1 misses or balls up the closing quote ([/quot] for eg)

then surly it would cock everything else up?

EDIT~~
also why are so many slashes needed? 0.o

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 1:11 am
by Torniquet
i have just found a bb code parser which looks slightly easier to understand and edit (no offence falk <3)

Code: Select all

<?
    $bbtags=array
    (
      '#\[b\](.*?)\[/b\]#msi'               => '<strong>\1</strong>',
      '#\[i\](.*?)\[/i\]#msi'               => '<em>\1</em>',
      '#\[u\](.*?)\[/u\]#msi'               => '<u>\1</u>',
      '#\[s\](.*?)\[/s\]#msi'               => '<s>\1</s>',
      '#\[url\](.*?)\[/url\]#msi'           => '<a href="\1">\1</a>',
      '#\[url=(.*?)\](.*?)\[/url\]#msi'     => '<a href="\1">\2</a>',
      '#\[img=(.*?)\]#msi'                  => '<img src="\1">',
      '#\[font=(.*?)\](.*?)\[/font\]#msi'   => '<font face="\1">\2</font>',
      '#\[size=(.*?)\](.*?)\[/size\]#msi'   => '<font size="\1">\2</font>',
      '#\[color=(.*?)\](.*?)\[/color\]#msi' => '<font color="\1">\2</font>',
    );

$safe=preg_replace(array_keys($bbtags), array_values($bbtags), $safe); 
?>  
this was sourced from http://codingforums.com/showthread.php?t=41003

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 2:42 am
by Jackolantern
Torniquet wrote: and if some1 misses or balls up the closing quote ([/quot] for eg)

then surly it would cock everything else up?
That is pretty much how every forum works right now. Try leaving out a [/quote] and it will mess it all up. However, you would probably have to put something at the end to end the div in case the user forgot to close it.

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 3:14 am
by ZeroComp
Arghhhhh my brain hurts from all the /'s :lol: nice job falk

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 4:01 am
by Torniquet
Jackolantern wrote:
Torniquet wrote: and if some1 misses or balls up the closing quote ([/quot] for eg)

then surly it would cock everything else up?
That is pretty much how every forum works right now. Try leaving out a [/ quote] and it will mess it all up. However, you would probably have to put something at the end to end the div in case the user forgot to close it.
No if you miss something or much it up, it just prints the bb code rather than the html tags

as demonstrated below.
[/quote

now by what i am guessing, in the one i have posted, it only creates the tags if both are present and correct. the whole '(.*?)' thing i presume is what text is contained between the tags.

i dont really know how to explain it lol, i think i just about understand it XD

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 4:44 am
by Jackolantern
Torniquet wrote:
Jackolantern wrote:
Torniquet wrote: and if some1 misses or balls up the closing quote ([/quot] for eg)

then surly it would cock everything else up?
That is pretty much how every forum works right now. Try leaving out a [/ quote] and it will mess it all up. However, you would probably have to put something at the end to end the div in case the user forgot to close it.
No if you miss something or much it up, it just prints the bb code rather than the html tags

as demonstrated below.
[/quote

now by what i am guessing, in the one i have posted, it only creates the tags if both are present and correct. the whole '(.*?)' thing i presume is what text is contained between the tags.

i dont really know how to explain it lol, i think i just about understand it XD
Hmmm, that's strange. In some other forums I have been on, if you forget the closing
, it just opens the quote and doesn't close it. I guess it wouldn't be too hard to add the functionality you want, however.

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 9:50 pm
by Falken
Torniquet wrote:No if you miss something or much it up, it just prints the bb code rather than the html tags
I guess you could keep a count on how many opening tags there are and then make sure there is a matching ending tag.

COnsidering all \\, you can read more about the preg_replace and patterns here:
http://php.net/manual/en/function.preg-replace.php

Can be really usefully, not only for BBCode, maybe for making data files and such for a game? :D

Re: [Script/Tutorial] BBCode

Posted: Tue Jan 12, 2010 9:59 pm
by Jackolantern
LOL oops, I screwed up my own post with wrong [..quote] tags lol