@KillahBeez: I'm not having much luck implementing what you wrote. If I said that currently I'm using PHP preg_replace('/\[([a-z]*?)\](.*?)\[([a-z]*?)\](.*?)\[\/\1\]/is','($1)$2($3)$4(/$3)(/$1)',$text);
Would you be able to better clue me in on what I might try to put in there? I gather the first (?xis) indicates that I use /xis at the end of the regex. Is the third section the replacement string? I tried preg_replace('/\[(b|u|i|s)\](?:(?:(?!\[/?(?2)\]).)++|(?1))+\[/\2\]/xis','(?!\s*\[/(?2)\])',$text); but it just errored about Unknown modifier '?' in the regex.
@prometheuzz
Sorry, PHP, using preg_replace, which is PCRE. So every regex I say here also starts with / and ends with /is
Now for the desired output. I plan to run through 2 replacements. One to ensure proper nesting, and the second to replace with the actual html tags. The second half is done, so here I'll just give the desired output for the first replacement (anything in bold indicates what changed):
1) (b)Bold(/b)(i)Italic(/i)
2) (b)(i)Both(/i)(/b)
3a) (b)(i)Alternating tags(/i)(/b)
3b) (b)(i)Alternating tags(/i)(/b)(/i)
(either would be acceptable, since the second replacement algorithm ignores unmatched end tags)
4a) (b)(i)Misplaced tag(/i)(/b)
4b) (b)Misplaced tag(/b)
4c) (b)(i)Misplaced tag(/b)
(any would be acceptable, since the second replacement algorithm would simply ignore the unmatched (i) tag)
5) (b)(i)(u)(s)All tags(/s)(/u)(/i)(/b)
6a) (b)(i)(u)(s)Swapped end tags(/s)(/u)(/i)(/u)(/b)
6b) (b)(i)(u)(s)Swapped end tags(/s)(/u)(/i)(/b)
6c) (b)(u)(i)(s)Swapped end tags(/s)(/i)(/u)(/b)
6d) (b)(i)(u)(s)Swapped end tags(/s)(/u)(/i)(/b)
(any combination thereof, as long as all matched start and end tags are properly nested. As above, unmatched tags are acceptable, since the second replacement algorithm would ignore them)
After my second replacement algorithm runs through it, it will find all matched tags and replace them with their html equivelents. I've already coded this.The end result for examples 3 and 4 would be thus:
3a) <b><i>Alternating tags</i></b>
3b) <b><i>Alternating tags</i></b>(/i)
4a) <b><i>Misplaced tag</i></b>
4b) <b>Misplaced tag</b>
4c) <b>(i)Misplaced tag</b>