Home >Backend Development >PHP Problem >How to get only Chinese in php
php method to only get Chinese: first create a PHP sample file; then pass "preg_match_all('/[\x{4e00}-\x{9fff}] /u', $contens, $content) ;" method can extract all Chinese characters in the string.
The operating environment of this article: Windows 7 system, PHP 7.1 version, DELL G3 computer
How can I only read Chinese in php?
php Extract all Chinese characters in the string
The code is as follows:
$contens ="assaujms提sd取(*&汉df字99876#$%^&"; //preg_match_all 函数用于执行一个全局正则表达式匹配 preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $contens, $content); $string = implode('',$content[0]); echo $string; //提取汉字
Related function introduction:
preg_match_all function is used to execute a global regular expression Expression matching.
Syntax
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
Search for all matching results in subject that match the given regular expression pattern and output them to matches in the order specified by flag.
After the first match is found, the subsequence continues to search from the last matching position.
Parameter description:
$pattern: The pattern to be searched, in string form.
$subject: input string.
$matches: Multi-dimensional array, output all matching results as output parameters, array sorting is specified by flags.
$flags: Can be used in combination with the following flags (note that PREG_PATTERN_ORDER and PREG_SET_ORDER cannot be used at the same time):
PREG_PATTERN_ORDER: The results are sorted into $matches[0] to save all matches of the complete pattern, $matches[ 1] Save all matches of the first subgroup, and so on.
PREG_SET_ORDER: The results are sorted as $matches[0] contains all matches (including subgroups) from the first match, $matches[1] contains all matches (including subgroups) from the second match ), and so on.
PREG_OFFSET_CAPTURE: If this flag is passed, each found match is returned with its offset relative to the target string increased.
offset: Usually, the search starts from the beginning of the target string. The optional parameter offset is used to start searching from the specified position in the target string (unit is bytes).
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get only Chinese in php. For more information, please follow other related articles on the PHP Chinese website!