I would have a look at the PEAR HTML_BBCodeParser package. The syntax for BBCode is slightly different from that found on P2P. But it's an already built solution! I've used this package, and it does have some limitations, but it's a great start.
http://pear.php.net/package/HTML_BBCodeParser
I also have a
JS program that inserts bbcode like that found on P2P ported to PHP functions for flexibility, in fact mine is based on the Snitz code. Email me and I'll send you a copy of it.
Regards,
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::