Pretty much some solutions
<?php
function show($s) {
static $i = 0;
echo "<pre>************** Option $i ******************* \n" . $s . "</pre>";
$i++;
}
$string = 'A [b]famous group[/b] once sang:
[quote]Hey you,[/quote]
[quote mlqksmkmd]No you don\'t have to go[/quote]
See [url
http://www.dailymotion.com/video/x9e7ez_pony-pony-run-run-hey-you-official_music]this video[/url] for more.';
// Option 0
show($string);
// Option 1: This will strip all BBcode without ungreedy mode
show(preg_replace('#\[[^]]*\]#', '', $string));
// Option 2: This will strip all BBcode with ungreedy mode (Notice the #U at the end of the regex)
show(preg_replace('#\[.*\]#U', '', $string));
// Option 3: This will replace all BBcode except [quote] without Ungreedy mode
show(preg_replace('#\[((?!quote)[^]])*\]#', '', $string));
// Option 4: This will replace all BBcode except [quote] with Ungreedy mode
show(preg_replace('#\[((?!quote).)*\]#U', '', $string));
// Option 5: This will replace all BBcode except [quote] with Ungreedy mode and mutiple lines wrapping
show(preg_replace('#\[((?!quote).)*\]#sU', '', $string));
?>
So actually, it's just a choice between option 3 and 5 I think.
[^]]
selects every char which isn't a ]
. It allows to "emulate" ungreedy mode.
U
regex option allows us to use .*
instead of [^]]*
s
regex option allows to match on multiple lines
(?!quote)
allows us to say anything that doesn't match "quote" in the next selection.
It is used this way: ((?!quote).)*
. See Regular expression to match a line that doesn't contain a word? for more info.
This fiddle is a live demo.