How to remove non-Chinese letters in php

青灯夜游
Release: 2023-03-15 19:40:02
Original
1603 people have browsed it

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)".

How to remove non-Chinese letters in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str= &#39;php中文网!-=1548&#39;;
$pattern = "/[^\x{4E00}-\x{9FFF}]+/u";
echo preg_replace($pattern,&#39;&#39;, $str);
?>
Copy after login

How to remove non-Chinese letters in php

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]])
Copy after login

Parameter description is as follows:

  • $pattern: to be searched Pattern can be a string or string array;
  • $replacement: string or string array for replacement. If this argument is a string and $pattern is an array, then all patterns are replaced with this string. If $pattern and $replacement are both arrays, each $pattern is replaced with the corresponding element in $replacement. If there are fewer elements in $replacement than in $pattern, the extra $pattern is replaced with the empty string.
  • $subject: The string or string array to be searched and replaced. If $subject is an array, the search and replacement will be performed on each element of $subject, and the return value will also be one array.
  • $limit: Optional parameter, the maximum number of substitutions per pattern on each $subject. Default is -1 (infinite).
  • $count: Optional parameter, if specified, will be filled with the number of completed substitutions.

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template