PHP and Unicode signature (BOM) _php Tutorial

Source: Internet
Author: User
Tags ultraedit
When writing or modifying a PHP file saved with UTF-8 encoding, there are sometimes some problems:
1. The page shows a "nobelium" word, the other blank;
2. Cannot log in or log out;
3. A blank appears at the top of the page;
4. Error warning on top of page;
5. Other abnormal circumstances;
6. The resulting picture browser is not recognized.
Analysis Reason:
When a file is saved in UTF-8 encoding, there are two situations: with a Unicode signature (BOM) and without a Unicode signature. BOM information is a string of hidden characters at the beginning of a file that allows some editors to recognize that this is a UTF-8 encoded file.

Software such as Windows-brought Notepad, when saving a UTF-8 encoded file, inserts three invisible characters (0xEF 0xBB 0xBF, Bom--byte Order Mark) where the file begins. It is a string of hidden characters that allows editors such as Notepad to identify whether the file is encoded in UTF-8. For general files, this does not cause any trouble.

But for PHP, PHP at design time does not consider the problem of the BOM, do not ignore the UTF-8 encoded file at the beginning of the BOM three characters, will be the BOM as part of the text at the beginning of the file. Because of the need to
Workaround:

When editing or changing any text file, be sure to use an editor that does not mess with the BOM. The editor under Linux should not have this problem. Under Windows, do not use editors such as Notepad. The recommended editor is: EditPlus 2.12 or above, Emeditor;ultraedit (need to cancel the "Add BOM" option), Dreamweaver (need to cancel the ' Add BOM ' related options), etc.

For a file that has already been added to the BOM, you can save it with the above editor if you want to cancel it. (EditPlus needs to be saved as GB first and then saved as UTF-8.) )

UltraEdit, EditPlus, notepad four tools support for UTF-8 is not the same, the following is a summary of the four tools for UTF-8 support:
UTF-8 BOM Header: is a three character: EF BB BF.
1. Notepad
Notepad when saving, choose UTF-8 Format, will write the BOM header on the file header.
2. EditPlus
When the file is saved, the UTF-8 format is selected, and the BOM header is not written on the file header.
3. UltraEdit
UltraEdit's support for UTF-8 is the most complete. In Advanced->configuration, you can choose whether to write the BOM header when the file is saved.
4. VI
Refers to the Linux under the Vim, if the UTF-8 file at the beginning of the BOM header, it can normally display UTF-8 encoding, otherwise, displayed as garbled.
There are also some encoding conversion tools, such as Java to write a simple code conversion tool, these tools will not increase the BOM header.

Appendix:

Ultreedit is configured, the option "Save on all UTF-8 write UTF-8 header tag (BOM)" is turned off. That is: Write UTF-8 BOM header to all UTF-8 files when saved OFF.
Different versions may not be, but the "automatic detection UTF8" to turn off. So look at the utf8 file is garbled.
It is advisable to use EditPlus.


Detects if the file in the directory has a BOM program:
This file is used to quickly test whether UTF8 encoded files are added to the BOM and can be automatically removed
by Bob Shen

$basedir = ".";; Modify this behavior to detect the directory where the point represents the current directory
$auto = 0; Whether the discovered BOM information is automatically removed. 1 for Yes, 0 for No.

Do not change the following

if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file! = '. ' && $file! = ': ' &&!is_dir ($basedir. " /". $file)" echo "FileName: $file". Checkbom ("$basedir/$file"). "
";
}
Closedir ($DH);
}

function Checkbom ($filename) {
Global $auto;
$contents =file_get_contents ($filename);
$charset [1]=substr ($contents, 0, 1);
$charset [2]=substr ($contents, 1, 1);
$charset [3]=substr ($contents, 2, 1);
if (Ord ($charset [1]) ==239 && ord ($charset [2]) ==187 && ord ($charset [3]) ==191) {
if ($auto ==1) {
$rest =substr ($contents, 3);
Rewrite ($filename, $rest);
Return ("BOM found, automatically removed.");
} else {
Return ("BOM found.");
}
}
else return ("BOM not Found.");
}

function rewrite ($filename, $data) {
$filenum =fopen ($filename, "w");
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>

http://www.bkjia.com/PHPjc/364307.html www.bkjia.com true http://www.bkjia.com/PHPjc/364307.html techarticle when writing or modifying a PHP file saved with UTF-8 encoding, some problems sometimes arise: 1. The page shows a nobelium word, the other blank; 2. Cannot log in or not login ...

  • 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.