I want to replace non-word non-space characters with spaces. There can be many symbol characters consecutively, so I'm using the regex below
[^\s\w]*
So, the code I'm using is:
$str = 'email@domain.com';
echo preg_replace('/[^\s\w]*/', ' ', $str);
I was expecting to get email domain com
but I get e m a i l d o m a i n c o m
, whitespaces between each character. If I remove the asterisk like following
echo preg_replace('/[^\s\w]/', ' ', $str);
then I get the desired email domain com
. But if input is email@@domain.com
then this regex will give email domain com
with two whitespaces between email
and domain
, which is not what I want.
Can anyone explain why [^\s\w]*
doesn't work, and how can I achieve what I want?