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