You are here:   Home > Scripts > PHP BBCode Parser

Warning: include(/var/www/includes/new-header.inc) [function.include]: failed to open stream: No such file or directory in /home/elouai/public_html/bbcode-sample.php on line 13

Warning: include() [function.include]: Failed opening '/var/www/includes/new-header.inc' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/elouai/public_html/bbcode-sample.php on line 13

Back to Scripts

BBCode Parser


The main goal behind bb2html.php was speed and simplicity thus the syntax was made more rigid to accomodate those goals (see below). To achieve this speed we exclusively used the function str_replace over preg_replace (regular expression). Benchmarking those two functions shows around a 3 to 4 fold speed increase.

  • The bbcode [list] does NOT take parameters and only produces the unordered HTML list
  • The bbcode [url] REQUIRES the parameter url-address to be defined WITHIN the tag and enclosed in quotes
  • The bbcode [mail] REQUIRES the parameter mailto to be defined WITHIN the tag and enclosed in quotes
  • The bbcode [color] REQUIRES the parameter color to be enclosed in quotes
  • The bbcode [size] REQUIRES the parameter size to be enclosed in quotes
  • The bbcode [quote] does NOT take parameters
  • The bbcode MUST be lowercase


Usage

Just include the string
<?php include 'bb2html.php';?>
And to call it simply write
<?php $htmltext = bb2html($bbtext);?>



Is this a one pass parser?

Not yet, however syntax wise, it allows for the possiblity. The current implementation of str_replace in PHP does a pass for EACH array element. In future versions of PHP str_replace may be optimized to using an internal binary tree lookup (or something similar) which would then require a single pass, and that in turn would make this a one-pass parser. In the meantime I am entertaining thoughts of creating a real one-pass lexer in PHP. (Will post once it is done)



BBCode quick reference

[img]http://elouai.com/images/star.gif[/img]
[url="http://elouai.com"]eLouai.com[/url]
[mail="webmaster@elouai.com"]email webmaster[/mail]
[size="25"]HUGE[/size]
[color="red"]RED[/color]
[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[list][*]item[*]item[*]item[/list]
[code]value="123";[/code]
[quote]John said yadda yadda yadda[/quote]


eLouai.com
email webmaster
HUGE
RED
bold
italic
underline
  • item
  • item
  • item

value="123";
John said bbcode script
Download bbcode to html parser

References:
PHP.net str-replace function
Zend.com str-replace function


Warning: include(/var/www/includes/footer.inc) [function.include]: failed to open stream: No such file or directory in /home/elouai/public_html/bbcode-sample.php on line 226

Warning: include() [function.include]: Failed opening '/var/www/includes/footer.inc' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/elouai/public_html/bbcode-sample.php on line 226