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

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

Question

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?

Was it helpful?

Solution

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

$str = preg_replace('#\[(?!/?img).+?]#im','',$str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top