I\'m trying to parse BBCode in my script. Now, it works seamelessly, until I try to indent BBCode that\'s more than just bold or underline - such as spoiler, url, font size,
As gordon said in comments PHP has a BBCode parser, so no reason to reinvent the wheel.
The native parser is a PECL package though, so you will have to install it. If that's not an option (for instance due to shared hosting), there is also a PEAR package: http://pear.php.net/package/HTML_BBCodeParser
In addition to those, you can take a look at forums using BB code source code, and either use their parser, or improve it. There is also several PHP implementations listed at http://www.bbcode.org/implementations.php
Correctly parsing BBcode using regex is non-trival. The codes may be nested. CODE
tags may contain BBCodes which must be ignored by the parser. Certain tags may not appear inside of other tags. etc. However, it can be done. I recently overhauled the BBCode parser for the FluxBB open source forum software. You may want to check it out in action:
New 2011 FluxBB Parser
Note that this new parser has not yet been incorporated into the FluxBB codebase.