[Script/Tutorial] BBCode

Post all your tuts or request for tuts here.
Post Reply
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

[Script/Tutorial] BBCode

Post 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 :)
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: [Script/Tutorial] BBCode

Post by hallsofvallhalla »

this is very nice. Thanks Falk!
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: [Script/Tutorial] BBCode

Post 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
New Site Coming Soon! Stay tuned :D
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: [Script/Tutorial] BBCode

Post 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
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Script/Tutorial] BBCode

Post 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.
The indelible lord of tl;dr
ZeroComp
Posts: 648
Joined: Thu Oct 29, 2009 8:48 pm

Re: [Script/Tutorial] BBCode

Post by ZeroComp »

Arghhhhh my brain hurts from all the /'s :lol: nice job falk
Coding-PHP, Javascript, C++, HTML, mySQL
Modeling/Art-Blender, GIMP
Games-Unity, Game Maker 8
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: [Script/Tutorial] BBCode

Post 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
New Site Coming Soon! Stay tuned :D
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Script/Tutorial] BBCode

Post 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.
The indelible lord of tl;dr
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: [Script/Tutorial] BBCode

Post 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
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Script/Tutorial] BBCode

Post by Jackolantern »

LOL oops, I screwed up my own post with wrong [..quote] tags lol
The indelible lord of tl;dr
Post Reply

Return to “Tutorials”