Looking for a BBCode parser (php)

May 4, 2011 at 4:52 PM

First of all - thank you for this great script. So far I was using a TinyMCE on my website but the problem was html tags. Many users are like children and make their profile texts like Christmas trees and usualy they copy formated texts somewhere. It leads the situation that some texts contains 90% of useless html tags like:

<p></p><p></p><p></p><span></span>span></span>span></span>

(the same as in this forum if you look at formatted text.)

 

 

WYSIWYG BBCode Editor is great - it allows to format texts in much cleaner way.

 

What I need is help to parse the BBCode. This is the function what I use:

 

 

function bbcode ($message) {
$search = array(
'@\[(?i)b\](.*?)\[/(?i)b\]@si',
'@\[(?i)i\](.*?)\[/(?i)i\]@si',
'@\[(?i)u\](.*?)\[/(?i)u\]@si',
'/\[color=rgb(.*?)\](.*?)\[\/color\]/si',
'/\[quote=](.*?)\[\/quote\]/si',
'/\[li](.*?)\[\/li\]/si',
'/\[ul](.*?)\[\/ul\]/si',
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<span style=\"color:rgb\\1\">\\2</span>',
'<span class=\"quote">\\1</span>',
'<li>\\1</li>',
'<ul>\\1</ul>',
);
return preg_replace($search , $replace, $message);
}



It works but not always. For example, from this:

asasas
asasasa

[ul][li]asasasas[/li][li]asasas[/li][li]asasa[/li][/ul]
asasasa



I get:
======================

 

asasas
asasasa

  • asasasas
  • asasas [li]asasa[/li ]


asasasa

===================

 

There are also problems to parse BBCode like this:

 

[b][color=rgb(102, 0, 102)]te[color=rgb(102, 153, 51)]s[/color]t[/color][/b]

 

where the result is:

===============

te[color=rgb(102, 153, 51)]st[/color]

===============

Where can I get a good BBCode parser? Can somebody help? I will pay some money if it's  necessary.

 

Thanks.