cahva;10922542 wrote:$trs="comment t'appelles-tu";
$newstr = preg_replace( '/\W/', ' ', $trs );
..will print "comment t appelles tu"
One must exercise caution when using shorthand character classes like \w (or in this case, \W), as depending on your system's locale, you might not get what you bargined for (the following also applies to stuff like \d, and ctype_digit, ctype_alnum and friends).
We typically expect \w = [a-zA-Z0-9_] but might not always be the case (my locale offers all that plus accented characters and exponents - and as a consequence, \W is also potentially altered). It really boils down to your local.
By example, my system's LC_CTYPE does not default to 'C' (it turns out to be set to 'English_United States.1252' - you can find out by using echo setlocale(LC_ALL, 0); ). As a result, it actually works out for me (if the string example contains accented characters):
$str = 'Le bébé est né mardi.';
$str = preg_replace('#\W#', ' ', $str);
echo $str; // Output: Le bébé est né mardi
But if the locale in question in fact treats \w as strictly [a-zA-Z0-9_] (that is to say, its LC_CTYPE is set to 'C'), then when re-attempting the above;
$str = 'Le bébé est né mardi.';
$str = preg_replace('#\W#', ' ', $str);
echo $str; // Output: Le b b est n mardi
So depending on allowable characters and your locale, things might throw you off.