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

WBOY
Release: 2016-06-23 13:54:53
Original
1441 people have browsed it

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)
Copy after login

全角的标点不当做汉字



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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!