Home >Backend Development >PHP Problem >How to use regular expressions in php to match only Chinese characters

How to use regular expressions in php to match only Chinese characters

青灯夜游
青灯夜游Original
2023-02-24 09:49:253566browse

In PHP, you can use the regular expression "/[\x{4e00}-\x{9fff}] /u" and the preg_match_all() function to match only Chinese characters, the syntax "preg_match_all("/[ \x{4e00}-\x{9fff}". The preg_match_all() function will search for all results in the string that can match the regular expression, matching "/[\x{4e00}-\x{9fff}] /u "You can filter the string and only get Chinese characters.

How to use regular expressions in php to match only Chinese characters

The operating environment of this tutorial: Windows 7 system, PHP 8 version, DELL G3 computer

In php, you can use the regular expression "/[\x{4e00}-\x{9fff}] /u" and the preg_match_all() function to match only Chinese characters.

preg_match_all() function will search for all results in the string that can match the regular expression

preg_match_all(pattern,subject,matches,flags,offset)

The parameter description is as follows:

  • pattern: the pattern to be searched, also It is a defined regular expression;
  • subject: the string to be searched;
  • matches: optional parameters (multi-dimensional array), used to store all matching results, the array is sorted by $ flags specified;
  • flags: optional parameters, which 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 by $matches[0] Save complete All matches of the pattern, $matches[1] saves all matches of the first subgroup, and so on.
    • PREG_SET_ORDER: The results are sorted as $matches[0] contains all matches obtained from the first match ( including subgroups), $matches[1] is an array containing all matches (including subgroups) found in the second match, and so on.
    • PREG_OFFSET_CAPTURE: If this flag is passed, each found When a match is returned, it will increase its offset relative to the target string. Note that this will change each match result string element in $matches, making it a match result string with the 0th element and a match result string with the 1st element. The offset of the matching result string in the subject.
  • offset: Optional parameter, $offset is used to start searching from the specified position in the target string (unit is bytes).

The preg_match_all() function can return the number of matches of pattern (possibly 0), or FALSE if an error occurs.

preg_match_all() function can filter strings with the regular expression "/[\x{4e00}-\x{9fff}] /u", only Get Chinese characters.

Note: preg_match_all() function will store the matching function characters one by one in an array (the array is specified by the third parameter).

<?php
header("Content-type:text/html;charset=utf-8");
$str = "欢迎4546来到php这里。zblog,我的?#$%^天呀&())*(&^";
echo $str;
preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);
var_dump($arr);
?>

How to use regular expressions in php to match only Chinese characters

At this time, you can use the implode() function to splice the result value into a string.

implode(&#39;&#39;,$arr[0])

How to use regular expressions in php to match only Chinese characters

Extended knowledge: implode() function

implode() function returns a string composed of array elements.

implode(separator,array)
  • separator: Optional. Specifies what is placed between array elements. Default is "" (empty string).

  • array: required. Arrays to be combined into strings.

Return value: Returns a string composed of array elements.

There is also a function that has the same function as the implode() function: join(). The join() function is an alias of the implode() function.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use regular expressions in php to match only Chinese characters. For more information, please follow other related articles on the PHP Chinese website!

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