开发者

Removing non-alphanumeric characters from a string

开发者 https://www.devze.com 2022-12-24 23:17 出处:网络
I have a string in PHP and I want it to match开发者_运维知识库 the regex [A-Za-Z0-9]. How can I do this?I am assuming you meant, a-z instead of a-Z, inside of your regex, but you can use preg_replace

I have a string in PHP and I want it to match开发者_运维知识库 the regex [A-Za-Z0-9]. How can I do this?


I am assuming you meant, a-z instead of a-Z, inside of your regex, but you can use preg_replace

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

It takes as arguments the pattern ([a-zA-Z0-9]), replacement ("") and the subject ($string) and returns the new string ($new_string)


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


\W is a shortcut for [^a-Z0-9_]. May not be extremely helpful as it allows underscores too, but thought I'd let you know.

0

精彩评论

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