I have this function to parse bbcode -> html:
$this->text = preg_replace(array(
\'/\\[b\\](.*?)\\[\\/b\\]/ms\',
\'/\\[i\\](.*?)\\[\\/i\\]/ms\',
It's pretty safe to say it's nigh impossible to build a reliable way to convert html to bbcode with just a slew of regexes. Use a parser (DOMDocument for instance), remove invalid elements & attributes with xpath's & inspection and then recursively walk it creating a bbcode string on the way (or just ignore invalid tags / attributes on the way).