I am trying to write a function in PHP using preg_replace where it will replace all those characters which are NOT found in list. Normally we replace where they are found but this one is different.
For example if I have the string:
$mystring = "ab2c4d";
I can write the following function which will replace all numbers with *:
preg_replace("/(\d+)/","*",$mystring);
But I want to replace those characters which are neither number nor alphabets from a to z. They could be anything like #$*();~!{}[]|\/.,<>?' e.t.c.
So anything other than numbers and alphabets should be replaced by something else. How do I do that?
Thanks
You can use a negated character class (using
^
at the beginning of the class):Update: I mean, you have to use a negated character class and you can use the one I provided but there are others as well ;)
\W
matches a non-alpha, non-digit character. The underscore_
is included in the list of alphanumerics, so it also won't match here.should do if you can live with the underscore not being replaced. If you can't, use
You want to use a negated "character class". The syntax for them is
[^...]
. In your case just[^\w]
I think.The
\d
,\w
and similar in regex all have negative versions, which are simply the upper-case version of the same letter.So
\w
matches any word character (ie basically alpha-numerics), and therefore\W
matches anything except a word character, so anything other than an alpha-numeric.This sounds like what you're after.
For more info, I recommend regular-expressions.info.
Since PHP 5.1.0 can use
\p{L}
(Unicode letters) and\p{N}
(Unicode digits) that is unicode equivalent like\d
and\w
for latinpreg_replace("/[^\p{L}\p{N}]/iu", $replacement_string, $original_string);
/iu
modifiers at the end of pattern:i (PCRE_CASELESS)
u (PCRE_UTF8)
see more at: https://www.php.net/manual/en/reference.pcre.pattern.modifiers.phpTry