Question

I would like to remove all   if is not in front one of this character !?:;% with preg_replace ( I supose ) .

<div>&nbsp;Hello&nbsp;!
&nbsp;&nbsp;&nbsp;&nbsp;
Am I 100&nbsp;% clear&nbsp;?&nbsp;</div>

It should give me

<div>Hello&nbsp;! Am I 100&nbsp;% clear&nbsp;?</div>

Thanks in advance

Was it helpful?

Solution

Use negative lookahead:

$str = preg_replace('/&nbsp;(?![!?:;%])/', '', $str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top