开发者

PHP RegEx specify characters that I DO want?

开发者 https://www.devze.com 2022-12-31 04:36 出处:网络
How would I strip out all开发者_开发百科 characters from a string that does NOT contain: [a-zA-Z0-9\\-\\/_] ?

How would I strip out all开发者_开发百科 characters from a string that does NOT contain: [a-zA-Z0-9\-\/_] ?

In other words, I'd like to specify what I DO want rather than what I don't. Thanks.


Easiest way:

preg_replace("/[^a-zA-Z0-9-\/_]/", '', $string);

Another approach would be to do a match and then implode the matched values.


try the following

preg_replace("/[^a-zA-Z0-9-\/_]/", "", $string);


If you want to keep a "/" and "\"

preg_replace("/[^a-zA-Z0-9-\\\/_]/", '', $string);


Shortest way to do it:

echo(preg_replace('~[^\w-/]~i', '', 'H#el/-l0:0.'));

Outputs:

"Hel/-l00"
0

精彩评论

暂无评论...
验证码 换一张
取 消