$str = "& &svnips  ∴ ≈ osidnviosd & sopinsdo";
$regex = "/&[^\w;]/";
echo preg_replace($regex, "&", $str);
I’m trying to replace all un-encoded ampersands with encoded ones.
The problem is it’s removing the space between & and sopinsdo.
Any idea why?
Why use regex? Why not use
htmlspecialchars()?Note the fourth parameter. It tells it not to double encode anything. So basically, this will turn all
<into<, all>into>and all&that are not part of an existing entity into&But, if you must use regex, you could do:
Basically, it saves the non-word character and then adds it back…