PHP and Unicode Signature (BOM)_PHP Tutorial

WBOY
Release: 2016-07-21 14:55:54
Original
815 people have browsed it

When writing or modifying PHP files saved in UTF-8 encoding, some inexplicable problems sometimes occur:
1. The page displays the word "锘" and the rest is blank;
2. Unable to log in or Unable to log out;
3. A blank line appears at the top of the page;
4. An error warning appears at the top of the page;
5. Other abnormal situations;
6. The generated image cannot be recognized by the browser.
Analysis reasons:
When the file is saved in UTF-8 encoding, there are two situations: with Unicode signature (BOM) and without Unicode signature. The BOM information is a string of hidden characters at the beginning of the file, which is used to allow some editors to recognize that this is a UTF-8 encoded file.

When saving a file encoded in UTF-8, software such as Notepad that comes with Windows will insert three invisible characters (0xEF 0xBB 0xBF, or BOM——) at the beginning of the file. Byte Order Mark). It is a string of hidden characters used to let editors such as Notepad identify whether the file is encoded in UTF-8. For ordinary files, this will not cause any trouble.

But for PHP, PHP did not consider the BOM issue when designing. It will not ignore the three characters of the BOM at the beginning of the UTF-8 encoded file, and will use the BOM as part of the text at the beginning of the file. . Since the code after
Solution:

When editing or changing any text file, please be sure to use an editor that does not add BOM randomly. Editors under Linux should not have this problem. Under WINDOWS, please do not use editors such as Notepad. The recommended editors are: Editplus version 2.12 or above; EmEditor; UltraEdit (the relevant options of 'Add BOM' need to be cancelled); Dreamweaver (the relevant options of 'Add BOM' need to be cancelled), etc.

For files that have added BOM, if you want to cancel, you can use the above editor to save it once. (Editplus needs to be saved as gb first, and then saved as UTF-8.)

Ultraedit, editplus, notepad four tools have different support for UTF-8. The following is the support of UTF-8 for the four tools. Summary:
UTF-8 BOM header: is three characters: EF BB BF.
 1. notepad
When saving notepad, select UTF-8 format, BOM header will be written in the file header.
 2. editplus
When saving file, select UTF-8 format, it will not Write BOM header in the file header.
 3. ultraedit
ultraedit has the most complete support for UTF-8. In advanced->configuration, you can choose whether to write the BOM header when saving the file.
 4. vi
refers to vim under Linux. If there is a BOM header at the beginning of the UTF-8 file, it can display UTF normally. -8 encoding, otherwise, it will be displayed as garbled characters.
There are also some encoding conversion tools. For example, you can use java to write a simple encoding conversion tool. These tools will not add BOM headers.

Appendix:

UltreEdit configuration, turn off the option "Write UTF-8 file header mark (BOM) for all UTF-8 when saving". That is: Write UTF-8 BOM header to ALL UTF-8 files when saved OFF.
Different versions may not have it, so you need to turn off "automatic detection of utf8". If you look at the utf8 file this way, it will be garbled.
It is recommended to use editplus.


Detect whether the file in the directory has a BOM program:
//This file is used to quickly test whether the UTF8 encoded file has a BOM and can be automatically removed.
//By Bob Shen

$basedir=".";; //Modify the directory that needs to be detected for this behavior, the dot indicates the current directory
$auto=0; //Whether to automatically remove it Discovered BOM information. 1 means yes, 0 means no.

//No need to 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);
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364307.htmlTechArticleWhen writing or modifying PHP files saved with UTF-8 encoding, some problems sometimes occur inexplicably: 1. The page displays a word "锘" and the rest is blank; 2. Unable to log in or unable to log in...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template