During the PHP development process, you may encounter garbled characters when reading text files, which is extremely detrimental to the normal operation of the program. This article will introduce some possible causes of garbled characters and provide some solutions.
Text files support multiple encoding formats, including UTF-8, GBK, GB2312, etc. If the encoding format of PHP does not match the encoding format used in the file when reading a text file, garbled characters will result.
In PHP, you can use the mb_detect_encoding() function to detect the file encoding format, and then use the iconv() function to convert. For example, the following code can read a UTF-8 encoded text file:
$file = 'test.txt'; $contents = file_get_contents($file); $encoding = mb_detect_encoding($contents, mb_detect_order(), true); if ($encoding != 'UTF-8') { $contents = iconv($encoding, 'UTF-8', $contents); } echo $contents;
If the character set in the server environment is set incorrectly, it will also resulting in garbled characters. The character set can be set in the PHP configuration file (php.ini) or in the server configuration file. For example, in the php.ini file, you can set the following parameters:
default_charset = "UTF-8" mbstring.language = "Chinese" mbstring.internal_encoding = "UTF-8" mbstring.http_input = "auto" mbstring.http_output = "UTF-8"
If the format of the text file is incorrect, it will also cause garbled characters. For example, under the Windows platform, the newline character used in text files is CR LF (carriage return character feed), while under the Unix/Linux platform, the newline character used in text files is LF (line feed).
You can use PHP's file() function to read the file content and use the str_replace() function to replace newlines. For example, the following code can read a text file and replace newlines:
$file = 'test.txt'; $contents = file($file); $contents = str_replace(array(" ", ""), " ", $contents); echo implode(" ", $contents);
If the text file contains illegal characters, it will also cause Garbled characters. You can use PHP's preg_replace() function to filter illegal characters. For example, the following code can read a text file and filter illegal characters:
$file = 'test.txt'; $contents = file_get_contents($file); $contents = preg_replace('/[ ---]/', '', $contents); echo $contents;
The above are some reasons and solutions that may cause garbled characters when PHP reads text files. When developing PHP, we must always pay attention to the character encoding settings and file format specifications to avoid problems such as garbled characters.
The above is the detailed content of php reads text garbled. For more information, please follow other related articles on the PHP Chinese website!