Home > Article > Backend Development > How to use regular expressions to remove Chinese characters in php
php method to use regular expressions to remove Chinese: 1. Create a php sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff] *)/i','',$a);" The regular method can remove Chinese characters in the query results.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
php How to use regular expressions to remove Chinese?
PHP uses regular expressions to remove Chinese characters from strings
When I was building a foreign language website recently, I needed to remove Chinese characters from a certain query result. In fact, the PHP language is very Convenient, use the following method to easily remove Chinese characters!
For example:
$a = "Hello World! 你好世界!"; $result = preg_replace('/([\x80-\xff]*)/i','',$a); echo $result;
The output result is:
“Hello World!”
Is it very simple? !
Related introduction:
preg_match - Execute matching regular expression
Instructions
preg_match( string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0 ): int|false
Search for the regular expression given by subject and pattern A match of the expression.
Parameters
pattern
Pattern to search for, string type.
subject
Enter the string.
matches
If the parameter matches is provided, it will be populated as search results. $matches[0] will contain the text matched by the full pattern, $matches[1] will contain the text matched by the first captured subgroup, and so on.
flags
flags can be set to a combination of the following flag values:
PREG_OFFSET_CAPTURE
If this flag is passed, for each occurrence of a match it is returned The string offset (number of bytes relative to the target string) is appended. Note: This will change the array filled in the matches parameter so that each element becomes a string where the 0th element is the matched string and the 1st element is the offset of the matched string in the target string subject. .
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use regular expressions to remove Chinese characters in php. For more information, please follow other related articles on the PHP Chinese website!