preg_replace in PHP - regular expression for NOT c

2020-08-22 03:27发布

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

6条回答
不美不萌又怎样
2楼-- · 2020-08-22 03:44

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 ;)

查看更多
仙女界的扛把子
3楼-- · 2020-08-22 03:46

\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);
查看更多
Melony?
4楼-- · 2020-08-22 03:53

You want to use a negated "character class". The syntax for them is [^...]. In your case just [^\w] I think.

查看更多
forever°为你锁心
5楼-- · 2020-08-22 03:57

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楼-- · 2020-08-22 04:02

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

查看更多
ゆ 、 Hurt°
7楼-- · 2020-08-22 04:06

Try

preg_replace("/([^a-zA-Z0-9]+)/","*",$mystring);
查看更多
登录 后发表回答