Home >Backend Development >PHP Tutorial >【正则表达式】检测字符串只包含数字、字母、下划线、汉字,该如何解决

【正则表达式】检测字符串只包含数字、字母、下划线、汉字,该如何解决

WBOY
WBOYOriginal
2016-06-13 12:00:272543browse

【正则表达式】检测字符串只包含数字、字母、下划线、汉字
RT
要验证一下用户名,只包含数字、字母、下划线、汉字,要求即使打乱顺序也能测出来
------解决方案--------------------
'/^[\x7f-\xffA-Za-z0-9_]+$/'
------解决方案--------------------

$s = '%';<br />var_dump(preg_match('/^\w+$/u', $s)); //int(0)<br />var_dump(preg_match('/^[\x7f-\xffA-Za-z0-9_]+$/', $s)); //int(1)

全角的标点不当做汉字



引用:
'/^[\x7f-\xffA-Za-z0-9_]+$/'
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn