[英]Removing non-alphanumeric characters from a string
I have a string in PHP and I want it to match the regex [A-Za-Z0-9]. 我在PHP中有一个字符串,我希望它匹配正则表达式[A-Za-Z0-9]。 How can I do this? 我怎样才能做到这一点?
I am assuming you meant, az
instead of aZ
, inside of your regex, but you can use preg_replace
我假设你的意思是az
而不是aZ
,你的正则表达式,但你可以使用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
) 它采用模式( [a-zA-Z0-9]
),替换( ""
)和主语( $string
)作为参数,并返回新字符串( $new_string
)
$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
\\W
is a shortcut for [^a-Z0-9_]
. \\W
是[^a-Z0-9_]
的快捷方式。 May not be extremely helpful as it allows underscores too, but thought I'd let you know. 可能不是非常有用,因为它也允许下划线,但我想我会告诉你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.