Home > Article > Backend Development > PHP regular matching method for Chinese and English, numbers and underlines
This article mainly introduces the method of PHP regular matching of Chinese and English, numbers and underscores, which can be used to verify user names. Friends who need it can refer to it. I hope it can help everyone.
1. Question:
The user name when registering is required to be composed of Chinese and English, numbers or underscores, and must not contain other characters.
2. Solution:
Just perform regular verification of Chinese, English, numbers and underscores on the input characters.
The code is as follows:
$username='脚本之家jb51.net@#……^&*'; $preg='/[\w\x{4e00}-\x{9fa5}]+/u'; //$preg='/[\x80-\xff_\w]+/u'; preg_match($preg,$username,$arr); print_r($arr);
Running result:
Array ( [0] => 脚本之家jb51 )
Related recommendations:
Sharing tips on phpstorm regular matching to delete empty lines and comment lines
Detailed explanation of some regular matching expressions in jQuery
Detailed explanation of the method of matching Chinese with php and javascript
The above is the detailed content of PHP regular matching method for Chinese and English, numbers and underlines. For more information, please follow other related articles on the PHP Chinese website!