We need to understand first.
Chinese Double-byte character encoding range
1. GBK (gb2312/gb18030)
X00-xff GBK Two-byte coding range
x20-x7f ASCII
Xa1-xff Chinese gb2312
X80-xff Chinese GBK
2. UTF-8 (Unicode)
U4e00-u9fa5 (Chinese)
x3130-x318f (Korean
XAC00-XD7A3 (Korean)
u0800-u4e00 (Japanese)
Get the specified Chinese and character
Example 1
| The code is as follows |
Copy Code |
<?php
$str = ' How do I match the text in a Chinese string? ';
Preg_match ("/Medium/", $STR, $regs);
Print_r ($regs);
?>
|
Output
Array
(
[0] in =>
)
Example 2
| The code is as follows |
Copy Code |
|
<?php
$str = ' Here is the Chinese ' is 中文版 ';
Turn the Chinese into red
echo Preg_replace ("/([X80-xff].) /"," <font color=red>\1</font> ", $str);
Turn the non-Chinese into red
echo Preg_replace ("/([x20-x7f])/", "<font color=red>\1</font>", $str);
?>
|
Signed in Chinese, English, digital and other mixed-row, you need to special processing of Chinese characters, because Pcre does not support the U P l such as Perl string processing escape, using 16 or Unicode processing, examples are as follows:
| The code is as follows |
Copy Code |
|
$str = "The second broad Gan Jia Kou East # label 2 #标签3. #标签4, all hair $1234¥XC,CVM, ¥12,dflksjf How # Label 5.x #tag6. CVXCV "";
Preg_match_all ('/# ([a-za-z0-9x7f-xff]+)/', $STR, $mat);
Print_r ($mat);
Preg_match ("/[x{00a5}x{ffe5}] (d+)/U", $str, $mat);
Print_r ($mat); |
Finally, I understand the final correct expression of Chinese characters matched with regular expressions in utf-8 coding in PHP--/^[x{4e00}-x{9fa5}]+$/u
| The code is as follows |
Copy Code |
|
$STR = "PHP programming";
if (Preg_match ("/^[x{4e00}-x{9fa5}]+$/u", $str)) {
Print ("The string is all Chinese");
} else {
Print ("This string is not all Chinese");
} |
Instance
The following matching Chinese string regular expression is a use of an array, and then several times for the full Chinese string is printed.
*/
| The code is as follows |
Copy Code |
|
$str _arr = Array (
Iameverysorry,
"Happy programming, Happy Living",
"PHP Tutorial Programming",
"1997 Hong Kong Return",
"English learning abc",
"Www.111cn.net"
) ;
$patt _ch = Chr (0XA1). "-" . Chr (0xff);
foreach ($str _arr as $str)
{
echo string ' $str ' is ';
if (Preg_match ("/^[ $patt _ch]+$/", $str))
{
echo" <b> complete Chinese </b> ";
echo "<br>";
echo "<br>";
}
Else
{
echo "Non-complete Chinese";
echo "<br>";
echo "<br>";
}
} |
For more details see: http://www.111cn.net/phper/php-cy/34301.htm