Use negative lookahead:
$str = preg_replace('/ (?![!?:;%])/', '', $str);
Question
I would like to remove all if is not in front one of this character !?:;% with preg_replace ( I supose ) .
<div> Hello !
Am I 100 % clear ? </div>
It should give me
<div>Hello ! Am I 100 % clear ?</div>
Thanks in advance
Solution
Use negative lookahead:
$str = preg_replace('/ (?![!?:;%])/', '', $str);