Recently, in the use of file_get_contents function to obtain the content of the text, there is a situation (as follows), have been struggling with n long, not its solution, and finally, sure enough to rely on Baidu ah ....
Baidu to an explanation, the following is the original:
The file_get_contents function in PHP5 gets the contents of the file and is actually read by binary, so when you use file_get_contents to get a UTF-8 file with a BOM, it does not remove the UTF-8 BOM when you read
Content as text to do something, some unexpected results may occur. This does not count as a bug because the File_get_contents function reads the file by binary and reads the content that contains
BOM, and when the user is operating, it is assumed that the content read is not included in the BOM text content (such as open after Notepad), because the BOM is not visible in the editing software, only in the hexadecimal mode can see
See, the problem is here, in fact, because of "operation is not uniform" caused.
When the UTF-8 encoded file operation, if you want to read the content as text content to deal with, it is best to do some processing of the BOM, this problem is resolved in the PHP6 (can set the text/binary reading mode), interested friends
You can find the PHP6 manual for yourself.
A relatively simple workaround:
1 <? php 2 $dataStr = file_get_contents (' Test.txt ' 3 Strpos ( $dataStr , "\xef\xbb\xbf") = = = 0) { 4 $dataStr = substr ( $dataStr , 3 5 6 // action on $datastr 7 ?
Or use the regular to handle:
1 <? php 2 $dataStr = file_get_contents (' Test.txt ' 3 Preg_match ('/^\xef\xbb\xbf/', $dataStr 4 $ Datastr = substr ( $dataStr , 3< Span style= "color: #000000;" >); 5 6 // action on $datastr 7 ?
What is a BOM?
The BOM is the abbreviation for the byte order mark, the byte sequence mark, which is a special tag inserted at the beginning of the utf-8,utf-16 or UTF-32 encoded Unicode file to identify the encoding type of the Unicode file.
Several codes correspond to the BOM:
EF BB BF UTF-8
FE FF UTF-16 (Big-endian)
FF FE UTF-16 (Little-endian)
XX FE FF UTF-32 (Big-endian)
FF FE xx UTF-32 (Little-endian)
For UTF-8 encoded files, BOM tagging is optional, Windows comes with a Notepad file saved as UTF-8 encoding, will automatically add a BOM, now some editing software, can be saved as UTF-8 encoding can choose whether to take
BOM Save.
For PHP files, it is best not to save the BOM when using UTF-8 encoding. Because when you use include/require/include_once/require_once these functions to include a file with a BOM, you get the Web page, in some compatibility
Not very good browser, you will find that the actual display of your page is slightly different than expected.
PHP--file_get_contents function in PHP5 get utf-8 file contents with BOM