To use regular expressions to match Chinese characters in php, we need to understand the string encoding and the internal code of the Chinese characters. This way we can quickly and easily accurately match Chinese characters. Let me introduce it to you.
To determine whether a string is Chinese in php, you will follow this idea:
The code is as follows
|
Copy code
|
||||||||||||||||
$str = "php programming"; if (preg_match("/^[u4e00-u9fa5]+$/",$str)) { print("This string is all in Chinese");} else { print("This string is not all Chinese");} ?>
However, you will soon find that php does not support such expressions and an error message is reported: Warning: preg_match() [function.preg-match]: Compilation failed: PCRE does not support L, l, N, U,or u at offset 3 in test.php on line 3
I made a breakthrough in the way of expression and found that in php, x is used to represent hexadecimal data. So, is transformed into the following code:
} else { Here are two examples:
|