File in PHP5

Source: Internet
Author: User
The file_get_contents function in PHP5 obtains the file content, which is actually read in binary format. Therefore, when you use file_get_contents to obtain a UTF-8 file with BOM, it does not remove the BOM of the UTF-8, and some unexpected results may occur when you take the read content as the text content for some operations. This does not count

The file_get_contents function in PHP5 obtains the file content, which is actually read in binary format. Therefore, when you use file_get_contents to obtain a UTF-8 file with BOM, it does not remove the BOM of the UTF-8, and some unexpected results may occur when you take the read content as the text content for some operations. This does not count

The file_get_contents function in PHP5 obtains the file content, which is actually read in binary format. Therefore, when you use file_get_contents to obtain a UTF-8 file with BOM, it does not remove the BOM of the UTF-8, and some unexpected results may occur when you take the read content as the text content for some operations. This is not counted as a BUG, because the file_get_contents function reads files in binary format, and the read content contains BOM, it is assumed that the read content does not contain the text content of BOM (such as the content displayed after being opened in Notepad), because BOM is invisible in the editing software, this problem can be seen only in the hexadecimal mode. It is actually caused by "inconsistent operations.

When the UTF-8 encoding file operations, if you want to read the content as text content to process, it is best to first some of the BOM processing, this problem has been solved in PHP6 (you can set the text/binary read mode). If you are interested, you can find the PHP6 manual by yourself.

A simple solution:

[Php]View plaincopy

  1. $ DataStr = file_get_contents('test.txt ');
  2. If (strpos ($ dataStr, "\ xEF \ xBB \ xBF") = 0 ){
  3. $ DataStr = substr ($ dataStr, 3 );
  4. }
  5. // Perform operations on $ dataStr
  6. ?>

You can also use the regular expression to handle the problem:

[Php]View plaincopy

  1. $ DataStr = file_get_contents('test.txt ');
  2. If (preg_match ('/^ \ xEF \ xBB \ xBF/', $ dataStr )){
  3. $ DataStr = substr ($ dataStr, 3 );
  4. }
  5. // Perform operations on $ dataStr
  6. ?>

What is BOM?

BOM is the abbreviation of a Byte Order Mark, a Byte Order Mark, which is a special marker inserted at the beginning of a Unicode file that is inserted into a UTF-8, UTF-16, or UTF-32-encoded to identify the encoding type of a Unicode file.

BOM corresponding to several codes:

Ef bb bf UTF-8
Fe ff UTF-16 (big-endian)
Ff fe UTF-16 (little-endian)
00 00 fe ff UTF-32 (big-endian)
Ff fe 00 00 UTF-32 (little-endian)

For UTF-8-encoded files, BOM markup is dispensable, Windows built-in notepad files will automatically add BOM when saved as UTF-8 encoding, now some editing software, you can choose whether to save BOM when saving as UTF-8 encoding.

For PHP files, it is best not to save BOM when using UTF-8 encoding. Because when you use the include/require/include_once/require_once functions to include a file with BOM, the webpage you get is in some browsers with poor compatibility, you will find that the actual display effect of Your webpage is slightly different from the Expected One.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.