preg_replace search for string that does not match the pattern instead of string that match the pattern

StackOverflow https://stackoverflow.com/questions/17626266

I have this array of possible values that I want to replace with preg_replace function:

$attr = array('b','i','u','left','center','right');

foreach($attr as $a) {
    // strip bbcode
    $str = preg_replace('#\['.$a.'\](.*)\[/'.$a.'\]#im','$1',$str); 
}
return $str;

but instead of using foreach and looping through each possible exclusion, I want to do it like strip anything that isn't an img instead. Is this possible?

有帮助吗?

解决方案

Do you mean you want to strip out all tags except img tags? Use this:

$str = preg_replace('#\[(?!/?img).+?]#im','',$str);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top