Received a request today, user nickname system needs to limit the character type of the nickname input to Chinese, English, digital,-,_
Obviously this should be done with a regular, so what is the final rule? The sample code is as follows:
<? PHP $str = ' I love Beijing Tian ' an gate -1234567_abacabcddd '; if (! Preg_match ("/^[\x{4e00}-\x{9fa5}a-za-z0-9_-]+$/u",$str//UTF-8 Kanji alphanumeric underline regular expression { Echo "Illegal entry";} Else { echo ' nickname is legal ';}
Here the matching Chinese is utf-8 encoded, that if you want to match the GBK encoding of Chinese should be how to write it?
<? PHP $str = "Long Live Chairman Mao "; if (! Preg_match ("/^[". CHR (0XA1). " -". CHR (0xFF). "] +$/",$str///GBK encoding Zhong Wenjing match { echo " input illegal ";} Else { echo ' input is legal ';}
PHP Chinese regular match