Home >Backend Development >PHP Problem >How to remove non-Chinese letters in php
php method to remove non-Chinese letters: Use the preg_replace() function with regular expressions to search for non-Chinese letters in the string and replace them with empty characters. The syntax is "preg_replace("/ [^\x{4E00}-\x{9FFF}] /u",'', $str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use preg_replace () function cooperates with regular expressions to remove non-Chinese letters in strings.
The regular expression is:
/[^\x{4E00}-\x{9FFF}] /u
preg_replace () Deletion principle: Use regular expressions to search for non-Chinese letters in the string and replace them with empty characters ''
.
Implementation example:
<?php header('content-type:text/html;charset=utf-8'); $str= 'php中文网!-=1548'; $pattern = "/[^\x{4E00}-\x{9FFF}]+/u"; echo preg_replace($pattern,'', $str); ?>
Description: preg_replace():
preg_replace() function can perform regular expressions Expression search and replacement is a powerful string replacement processing function. The syntax format of this function is as follows:
preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
Parameter description is as follows:
If $subject is an array, the preg_replace() function will return an array, otherwise it will return a string.
If the function preg_replace() finds a match, it will return the replaced $subject, otherwise it will return the unchanged $subject. Each parameter of the preg_replace() function (except the parameter $limit) can be an array. If the $pattern parameter and the $replacement parameter are both arrays, the function will process the keys in the order they appear in the array. If an error occurs, NULL is returned.
The parameter $replacement can contain back references \\n or $n, the latter is preferred syntactically. Each such reference will be replaced by the text captured by the nth capturing subgroup that was matched. n can be 0-99, with \\0 and $0 representing the complete pattern matching text.
The serial number counting method of capturing subgroups is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use backslashes in $replacement, you must use 4 ("\\\\" because this is first a php string, and after escaping it is two, and then it is considered as a string after passing through the regular expression engine. an original backslash).
When working in replacement mode and the backreference needs to be followed by another number (for example: adding an original number immediately after a matching pattern), you cannot use the syntax \\1. Describes backreferences. For example, \\11 will make preg_replace() unable to understand whether you want a \\1 backreference followed by an original 1, or a \\11 backreference followed by nothing. The solution in this case is to use ${1}1. This creates a separate backreference for $1, a separate backreference for source 1.
When using the deprecated e modifier, this function will escape some characters (ie: ', ", \ and NULL) and then perform backreference replacement. When this is done please make sure to backreference After the reference is parsed, there are no syntax errors caused by single quotes or double quotes (for example: 'strlen(\'$1\') strlen("$2")'). Ensure that it conforms to PHP's string syntax and complies with eval syntax. Because in After completing the replacement, the engine will use the eval method to evaluate the result string as PHP code and use the return value as the final string participating in the replacement.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove non-Chinese letters in php. For more information, please follow other related articles on the PHP Chinese website!