Home >Backend Development >PHP Tutorial >Solution to the garbled problem of reading files in PHP

Solution to the garbled problem of reading files in PHP

WBOY
WBOYOriginal
2016-07-25 09:13:22965browse

Using php to read the file 3.txt, but there is a garbled problem. D:/3.txt is a utf-8 file. Code:

  1. $f1 = fopen('D:/3.txt','r');
  2. $str = fread($f1,10000);
  3. fclose($f1);
  4. echo substr($str,1,3);
Copy code

There is a BOM problem. In order to mark this text as UTF text, Microsoft added three bytes, which are: ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191 The above code is the php code to delete the BOM.

When intercepting, you can start intercepting from the fourth position. If you intercept from the first, second, or third digit, garbled characters may appear. In UTF-8 encoding, a Chinese character may be represented by three bytes. If there are Chinese characters, do not use ANSI encoding, otherwise garbled characters will appear when reading.

ANSI encoding (from the encyclopedia)

  Unicode and ansi are both representations of character codes. In order for the computer to support more languages, 2 bytes in the range of 0x80~0xFF are usually used to represent 1 character. For example: the Chinese character '中' is stored in the two bytes [0xD6,0xD0] in the Chinese operating system. Different countries and regions have formulated different standards, resulting in their own encoding standards such as GB2312, BIG5, and JIS. These various Chinese character extended encodings that use 2 bytes to represent a character are called ANSI encodings. Under Simplified Chinese systems, ANSI encoding represents GB2312 encoding, and under Japanese operating systems, ANSI encoding represents JIS encoding.

 Different ANSI codes are incompatible with each other. When information is exchanged internationally, text belonging to two languages ​​cannot be stored in the same ANSI coded text.

If it is English or symbols, then the encoding is 1 byte, and the highest bit is 0. If it is Chinese, the highest bit must be 1, and the size is 2 bytes. From this point of view, if there is Japanese or Korean in the text file that stores ANSI in our Chinese computer, it may cause encoding conflicts. That is to say, we cannot use ANSI encoding to store Chinese, Japanese and Korean in Notepad. mixed text. The notepad in the computer is developed for the Chinese version of the system. If you want it to be universal, you have to store the txt file into a Unicode format text file. Therefore, if you want to do international things, it is more convenient to use Unicode. In fact, most of today's operating systems are encoded in Unicode. If we use ansi encoding, we still have to convert it to Unicode during internal processing of the system, which in turn results in low code efficiency. It’s more convenient to use Unicode!

php garbled problem Code:

  1. $content=file_get_contents("http://bbs.it-home.org/");
  2. $pattern="/
  3. $match = array();
  4. preg_match_all($pattern,$content,$match);
  5. print_r($match);
Copy code

There is a garbled problem.

Add header("Content-type: text/html;charset=utf-8"); and that's it.

Used to set the html encoding format to utf-8

To solve garbled characters, look at three places: 1. Database encoding 2. Page encoding 3. Connection encoding

If these three places are consistent, there will be no garbled code problem.



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