I need some help with a regexp. I am trying to make a code parser (parses code similar to UBB code) but am having a bit of trouble with it.
Here is the code:
<?PHP
$message = "Doesn't work: [b][i][u]text[/u][/i][/b] Works: [b]text[/b]";
# bold
$message = preg_replace("/\[b\]([^\[]*)\[\/b\]/","<b>\\1</b>",$message);
# italics
$message = preg_replace("/\[i\]([^\[]*)\[\/i\]/","<i>\\1</i>",$message);
# underline
$message = preg_replace("/\[u\]([^\[]*)\[\/u\]/","<u>\\1</u>",$message);
echo $message;
?>
Ok, the forums really butchered that, but if you hit quote, you should get my original...
Anyway, what the problem is, is when I try to nest code. Take this example: (I am using { for [ and } for ] throughout this post due to the formatting of these forums)
{b}{i}{u}text{u}{i}{b} should print text but due to the way my code is written, it only parses the inner tags. I needed to write it that way, as if I just replace {b} with <b> and {/b} with </b> there will be issues if the user doesn't enter equal numbers of closing tags and if I don't check for ['s (the negative charater set...) it would parse code like this wrong:
{b}text{/b} more {b}text{/b}
You'll see what I mean by all this if you run the code with the above examples...
I'd appreciate any help anyone can give me, or if you know of a better way to parse UBB code, that would be great, as well...
Thanks!