Using PHP to read file 3.txt, but there is a garbled problem, D:/3.txt is the Utf-8 file. Code:
- $f 1 = fopen (' d:/3.txt ', ' R ');
- $str = fread ($f 1,10000);
- Fclose ($f 1);
- Echo substr ($STR, 1, 3);
Copy CodeThere is a BOM problem, Microsoft in order to mark this text as UTF text, add three bytes, respectively:ord ($charset [1]) = = 239 && ord ($charset [2]) = = 187 && Ord ($ CHARSET[3] = = 191 The code above is the PHP code to delete the BOM. Interception can start at the fourth position. If it is intercepted from the first to second or third position, garbled characters may occur. A Chinese character in UTF-8 encoding may be represented by three bytes. If there is a Chinese character, do not use ANSI code, otherwise read the time will appear garbled. ANSI code (inside the encyclopedia) Both Unicode and ANSI are representations of character codes. To enable the computer to support more languages, you typically use the 0x80~0xff range of 2 bytes to represent 1 characters. For example: Chinese characters ' in ' in the Chinese operating system, using [0xd6,0xd0] These two bytes of storage. Different countries and regions have developed different standards, resulting in GB2312, BIG5, JIS and other coding standards. These use 2 bytes to represent a character of a variety of Chinese character extension encoding, called ANSI encoding. Under the Simplified Chinese system, ANSI encoding represents GB2312 encoding, and in Japanese operating system, ANSI encoding represents JIS code. Different ANSI encodings are incompatible, and when information is exchanged internationally, text that is in two languages cannot be stored in the same piece of ANSI-encoded text. If the English or the symbol is encoded as 1 bytes, and the highest bit is 0, if it is Chinese then the highest level is certainly 1, 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 is possible to conflict with the encoding, that is, we can't store the mixed text of CJK in Notepad with ANSI encoding. The Notepad in the computer is developed for the Chinese version of the system. If you want to be generic, you have to store the TXT file in Unicode format text file. So, if you want to do something internationalized, it is convenient to use Unicode. In fact, the current operating system is mostly Unicode-encoded, if we are using ANSI encoding, then the internal processing of the system will have to convert to Unicode, but the code is inefficient. Or use Unicode convenient Ah! PHP garbled Problem Code:
- $content =file_get_contents ("http://bbs.it-home.org/");
- $pattern = "//IMSU";
- $match = Array ();
- Preg_match_all ($pattern, $content, $match);
- Print_r ($match);
Copy CodeThere was a garbled problem. Plus header ("Content-type:text/html;charset=utf-8"); You can do it. Used to set HTML encoding format is Utf-8 Garbled solution See three places: 1, Database Encoding 2, page encoding 3, Connection code If these three places are consistent, there will be no garbled problem. |