This article summarizes some example functions in PHP that use regular expressions to match Chinese. Friends in need can refer to them.
We must first understand
Chinese double-byte character encoding range
1. GBK (GB2312/GB18030)
x00-xff GBK double-byte encoding 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 characters
Example 1
The code is as follows td> |
Copy code |
| 代码如下 | 复制代码 |
$str = '正则如何匹配中文字在某中文字符串中?'; preg_match("/中/",$str,$regs); print_r($regs); ?>
|
$ str = 'How to match Chinese characters in a Chinese string with regular expressions? ';
preg_match("/中/",$str,$regs);
print_r($regs);
?>
Output
Array
(
[0] => in
代码如下 | 复制代码 |
$str = '这里是中文 this is English'; //把中文变成红色 echo preg_replace("/([x80-xff].)/","1",$str); //把非中文变成红色 echo preg_replace("/([x20-x7f])/","1",$str); ?>
|
)
Example 2
代码如下 | 复制代码 |
$str = "之二宽阔的甘家口东#标签1#标签2 #标签3。#标签4,都发34 ¥xc,cvm , ¥12,dflksjf如何#标签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);
|
The code is as follows | Copy code |
$str = 'This is English'; //Turn Chinese into red 代码如下 | 复制代码 | $str = "php编程"; if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) { print("该字符串全部是中文"); } else { print("该字符串不全部是中文"); } | echo preg_replace("/([x80- xff].)/","1",$str);//Turn non-Chinese characters into redecho preg_replace("/([x20-x7f ])/","1",$str);?> |
If the signature contains Chinese, English, numbers, etc., special processing is required for Chinese characters. Since PCRE does not support perl string processing escapes such as U P L, use hexadecimal or Unicode for processing. , an example is as follows:
The code is as follows | Copy code |
$str = "Second Broad Ganjiakou East#tag1#tag2#tag3. #tag4, all sent $1234 ¥xc,cvm, ¥12,dflksjfHow to #tag5.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 understood the regular expression matching under utf-8 encoding in php The final correct expression of Chinese characters——/^[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 in Chinese");} else {print ("This string is not all in Chinese");} |
Example
The following regular expression for matching Chinese strings uses an array, and then uses for multiple times to print out all Chinese strings.
*/
The code is as follows 代码如下 | 复制代码 |
$str_arr = array( "iameverysorry", "快乐编程,快乐生活", "php教程编程", "1997年香港回归", "英语学习abc", "www.bkjia.com" );
$patt_ch = chr(0xa1) . "-" . chr(0xff);
foreach ($str_arr as $str) { echo "字符串'$str' 是"; if (preg_match("/^[$patt_ch]+$/", $str)) { echo "完全中文"; echo " "; echo " "; } else { echo "非完全中文"; echo " "; echo " "; } }
| |
Copy code |
| $str_arr = array(
"iameverysorry",
"Happy programming, happy life",
"php tutorial programming", "Hong Kong's return in 1997",
"English learning abc",
"www.bkjia.com");$patt_ch = chr(0xa1) . "-" . chr (0xff);foreach ($str_arr as $str){ echo "String '$str' is"; if (preg_match("/^[$patt_ch] +$/", $str)) { echo "Complete Chinese"; echo "
"; echo "
} else { echo "
"; echo "
"; }} For more details, please see: http://www.bkjia.com/phper/php-cy/34301.htm
http://www.bkjia.com/PHPjc/444649.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444649.htmlTechArticleThis article summarizes some example functions in PHP that use regular expressions to match Chinese. Friends in need can refer to it refer to. We must first understand the Chinese double-byte character encoding range 1. GBK...
Statement: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