preg_replace(\"/\\[b\\](.*)\\[\\/b\\]/Usi\", \"$1\", \"Some text here... [b][b]Hello, [b]PHP![/b][/b][/b] ... [b]and here[/b]\");
Yes, a multi-pass approach is required if the elements are nested. This can be accomplished in one of two ways; matching from the inside out or from the outside in. Here are two tested scripts with fully commented regexes which illustrate each technique:
$1', $text);
printf("i[%d]=%s", $i++, $text);
}
?>
'''
Replace matching tags from the inside out:
i[0]=Some text here... [b][b]Hello, [b]PHP![/b][/b][/b] ... [b]and here[/b]
i[1]=Some text here... [b][b]Hello, PHP![/b][/b] ... and here
i[2]=Some text here... [b]Hello, PHP![/b] ... and here
i[3]=Some text here... Hello, PHP! ... and here
'''
$1', $text);
printf("i[%d]=%s", $i++, $text);
}
?>
'''
Replace matching tags from the outside in:
i[0]=Some text here... [b][b]Hello, [b]PHP![/b][/b][/b] ... [b]and here[/b]
i[1]=Some text here... [b]Hello, [b]PHP![/b][/b] ... and here
i[2]=Some text here... Hello, [b]PHP![/b] ... and here
i[3]=Some text here... Hello, PHP! ... and here
'''
Note the (?R) recursive expression used in the second approach.