Home >Backend Development >PHP Problem >What to do if php bom has Chinese garbled characters
Solution to php bom Chinese garbled code: 1. Open the corresponding PHP development file; 2. Add "$bom = pack("C3",239,187,191);$strcsv =$bom.$strcsv;" that is Can.
The operating environment of this article: windows7 system, PHP7.4 version, DELL G3 computer
What should I do if php bom has Chinese garbled characters?
The problem of garbled characters caused by BOM in PHP development
In the project, the csv file needs to be downloaded directly in PHP, and after the file is downloaded, garbled characters are displayed in excel. So to increase the BOM, the method is as follows:
$bom = pack("C3",239,187,191); //bom: xEFxBBxBF $strcsv =$bom.$strcsv;
The following is a more concise explanation taken from the Internet:
BOM description: There is a code called "ZERO WIDTH NO-BREAK SPACE" in the UCS encoding character, its encoding is FEFF. FEFF is a character that does not exist in UCS, so it should not appear in actual transmission. The UCS specification recommends that we transmit the characters "ZERO WIDTH NO-BREAK SPACE" before transmitting the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore, the character "ZERO WIDTH NO-BREAK SPACE" is also called BOM.
UTF-8 does not require a BOM to indicate the byte order, but you can use the BOM to indicate the encoding method. The UTF-8 encoding of the character "ZERO WIDTH NO-BREAK SPACE" is EF BB BF. So if the receiver receives a byte stream starting with EF BB BF, it knows that it is UTF-8 encoded. Windows uses BOM to mark the encoding of text files.
PHP did not consider the BOM issue when designing, which means that it will not ignore the three characters of the BOM at the beginning of the UTF-8 encoded file.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php bom has Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!