Source code raw phps
<?php
/**
* Nice recursive way to parse BB tags
*/
$input = "my html code [u] ooh a tag [i] deepe[b] deeperdeeper tag [/b]r tag [/i] foo bar! [/u] my html normal code";
function parseTagsRecursive($input) {
if (is_array($input) && isset($input[2])) {
switch($input[1]) {
case 'u':
$format = '<span style="text-decoration:underline">%s</span>';
break;
case 'i':
$format = '<span style="font-style:italic">%s</span>';
break;
case 'b':
$format = '<span style="font-weight:bold">%s</span>';
break;
}
$input = sprintf($format, $input[2]);
}
return preg_replace_callback('#\[(u|i|b)]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#', 'parseTagsRecursive', $input);
}
$output = parseTagsRecursive($input);
echo $output;
Nice script colder :)
I just tested it and works perfectly. When not closing a tag. Example: [b] string it just outputs [b] and not bold :)