问题
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
回答1:
You can use a negated character class (using ^
at the beginning of the class):
/[^\da-z]+/i
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 ;)
回答2:
You want to use a negated "character class". The syntax for them is [^...]
. In your case just [^\w]
I think.
回答3:
Try
preg_replace("/([^a-zA-Z0-9]+)/","*",$mystring);
回答4:
\W
matches a non-alpha, non-digit character. The underscore _
is included in the list of alphanumerics, so it also won't match here.
preg_replace("/\W/", "something else", $mystring);
should do if you can live with the underscore not being replaced. If you can't, use
preg_replace("/[\W_]/", "something else", $mystring);
回答5:
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.
回答6:
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 latin
preg_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.php
来源:https://stackoverflow.com/questions/4724732/preg-replace-in-php-regular-expression-for-not-condition