In PHP if you want the regular to get the characters in the string we need to know the page encoding, the regular match Chinese characters according to the page encoding differs slightly different: gbk/gb2312 encoding: [x80-xff>]+ or [xa1-xff]+] and UTF-8 code: [x{4e00}- X{9fa5}]+/u.
Cases
The code is as follows |
Copy Code |
Echo (Mb_eregi ("[X80-xff].", "Medium D")? "There": "None"). " Chinese characters "; Echo (Mb_eregi ("^ ([x80-xff].) +$ "," Chinese ")? "All are Chinese characters": ""); Take a look at all Chinese string functions |
Here's a sample of PHP to match:
The code is as follows |
Copy Code |
$STR = "Learning php is a happy thing. "; Preg_match_all ("/[x80-xff]+/", $str, $match); UTF-8 using: Preg_match_all ("/[x{4e00}-x{9fa5}]+/u", $str, $match); Print_r ($match); ?> Output: Array ( [0] = = Array ( [0] = Learn [1] = = is a happy thing. ) ) |
Regular Chinese characters
The code is as follows |
Copy Code |
$str = "How does eregi in PHP match Chinese characters"; if (Preg_match ("/^[". Chr (0x80). " -". Chr (0xff)." +$/", $str)) { echo "This is a plain Chinese string"; } else{ echo "This is not a pure Chinese character string"; } Preg_match_all ($pat,......) With Preg_replace ($pat,......) ...... |
Preg_match_all ("/(Kanji) +/ism", "I am a kanji, see what you do to me!" ", $m _a);
The beginning and end of the high and low levels of each code, then nature can write the regular, and directly 16-bit, what is the difficulty? Oh. Note, however, that in PHP, it means that 16 bits are X
We can also use this regular expression to determine whether it is a gb2312 character.
The code is as follows |
Copy Code |
$str = "little kid"; if (Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str)) { Print ($str. " Indeed all are Chinese characters "); } else { Print ($str. " This true TC is not all Chinese characters "); } ?> |
http://www.bkjia.com/PHPjc/628906.html www.bkjia.com true http://www.bkjia.com/PHPjc/628906.html techarticle in PHP If you want the regular to get the characters in the string we need to know the page encoding, the regular match Chinese characters according to the page encoding different and slightly difference: gbk/gb2312 code: [X80-x ...