Home  >  Article  >  Backend Development  >  How to remove non-Chinese characters in php

How to remove non-Chinese characters in php

藏色散人
藏色散人Original
2021-07-14 10:23:451742browse

php method to remove non-Chinese characters: first create a PHP sample file; then use the fgets() function to extract each line of data from the txt file; finally use the regular function preg_replace() to remove non-Chinese characters in each line. Can.

How to remove non-Chinese characters in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to remove non-Chinese characters in php?

php function preg_replace() regular removal of non-Chinese characters

The method of removing non-Chinese characters needs to be used in the project, and the organized data

$file = fopen("hb/j.txt","r+") or exit("Unable to open file!");
while(!feof($file)){
$line=fgets($file);
$pattern = "/[^\x{4E00}-\x{9FFF}]+/u";
echo preg_replace($pattern, '', $line);
echo "<br />";
}
fclose($file);

is taken out from the txt file Each line, and remove non-Chinese characters in each line.

1. Take out a line and use the fgets() function

2. Remove non-Chinese characters using the regular function preg_replace()

3. Regular rules for non-Chinese characters /[^\x{ 4E00}-\x{9FFF}] / , please note that in php, \u**** cannot be used to represent unicode characters, and \x{****}

4.u must be used to represent the modifier

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove non-Chinese characters in php. 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