CodeIgniter Forums
Advanced bbcode parser - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Advanced bbcode parser (/showthread.php?tid=32139)



Advanced bbcode parser - El Forum - 07-14-2010

[eluser]CoffeeBeanDesign[/eluser]
Hi guys,

I've written a bbcode parser and I think it can be improved. Consider the following code :

Code:
// email tag
$text = preg_replace_callback("'\[email\](.*?)\[/email\]'s", array($this, '_parse_bb_email'), $text);
$text = preg_replace_callback("'\[email=(.*?)\](.*?)\[/email\]'s", array($this, '_parse_bb_email'), $text);
        
// youtube tag
$text = preg_replace_callback("'\[youtube\](.*?)\[/youtube\]'s", array($this, '_parse_bb_youtube'), $text);
$text = preg_replace_callback("'\[youtube=(.*?)\](.*?)\[/youtube\]'s", array($this, '_parse_bb_youtube'), $text);

I've got about 10 of these 'special' bbcode tags ( over and above the standard b, i, u ect which are handled by a nice and easy preg_replace with search/replace arrays ). It struck me that this is a cack handed way to do these special tags - what I want to do is wrap these into a single function which checks the tag ( for example 'email' ), looks for and calls the parse method (in the class). The important thing is that all the tags have a possible second variable i.e.

Code:
[email][email protected][/email] creates "[email protected]"
[[email protected]]Email Link[/email] creates "Email Link" (links to address)

It struck me that if I can create a single function to check for the method (always prefixed with "_parse_bb_") it would be a lot simpler to add in extra special tags later.

So I guess it needs to be something like a preg_replace ("'\[(.*?)\](.*?)\[/(.*?)\]'s", $text) to find out what the tag is ( the bit between the brackets ) then do a method_exists, then call the correct parse function on the result?? I just can't get my head around how to do it.

Your help would be very much appreciated.