Current Location: Home> Latest Articles> PHP Regular Expression: How to Precisely Match Chinese Characters

PHP Regular Expression: How to Precisely Match Chinese Characters

M66 2025-07-07

PHP Regular Expression: How to Precisely Match Chinese Characters

In PHP development, regular expressions are widely used for text matching and filtering tasks. When we need to extract Chinese characters from text, regular expressions are an extremely useful tool. This article introduces a common PHP regular expression method specifically for matching Chinese characters, with corresponding code examples provided.

Code Example

<?php

$pattern = '/[x{4e00}-x{9fa5}]+/u'; // Regular expression to match Chinese characters

$text = "Hello 你好 World 世界";

preg_match_all($pattern, $text, $matches);

foreach ($matches[0] as $match) {

echo $match . PHP_EOL;

}

Code Explanation

In the code above, we first define a regular expression $pattern to match Chinese characters. The part [x{4e00}-x{9fa5}] represents the Unicode range of all Chinese characters.

Next, we create a string $text that contains both Chinese and English characters. We then use the preg_match_all function to match the string, and store the matching results in the $matches array.

Finally, we use a foreach loop to print out all the matched Chinese characters.

Conclusion

Through the example code above, we can easily achieve the goal of matching only Chinese characters. It is important to note that you can adjust the regular expression rules according to your specific application needs to match different Chinese characters. We hope this content helps you better understand and utilize PHP regular expressions for matching Chinese characters.