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

【正则表达式】检测字符串只包含数字、字母、下划线、汉字

WBOY
WBOYOriginal
2016-06-23 13:54:531476browse

RT
要验证一下用户名,只包含数字、字母、下划线、汉字,要求即使打乱顺序也能测出来


回复讨论(解决方案)

规则串:/^\w+$/u
适用于 utf-8 字符集,非 utf-8 的,转成 utf-8 的再使用

'/^[\x7f-\xffA-Za-z0-9_]+$/'

$s = '%';var_dump(preg_match('/^\w+$/u', $s)); //int(0)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