PHP realizes the automatic detection and removal of the BOM of UTF-8 files,

Source: Internet
Author: User

PHP realizes the automatic detection and removal of the BOM of UTF-8 files,

This article describes the PHP to realize the UTF-8 file BOM automatic detection and removal method. Share it with you for your reference. The specific implementation method is as follows:

BOM information is a hidden string starting with a file that is used by some editors to identify it as a UTF-8-encoded file. However, PHP reads these characters when reading a file, which leads to some unrecognized characters at the beginning of the file.

For example, using the UTF-8 format to save the generated image PHP file, because the file header hidden BOM information is also issued, resulting in the generated image data is wrong, the browser cannot recognize.

To check whether a UTF-8 file contains BOM information, it is to check whether the file starts with three characters, 0xEF, 0xBB, 0xBF. The following applet traverses all files in a directory and checks whether BOM is added.
Copy codeThe Code is as follows: <? Php
// This file is used to quickly test whether the UTF-8 encoded file is added with BOM and can be automatically removed.
// By Bob Shen

$ Basedir = "."; // modify the directory to be checked for this behavior. The vertex indicates the current directory.
$ Auto = 1; // whether to automatically remove the detected BOM. 1 is yes, 0 is no.

// Do not change

If ($ dh = opendir ($ basedir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = '.' & $ File! = '..'&&! Is_dir ($ basedir. "/". $ file) echo "filename: $ file". checkBOM ("$ basedir/$ file"). "<br> ";
}
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 ("<font color = red> BOM found, automatically removed. </font> ");
} Else {
Return ("<font color = red> BOM found. </font> ");
}
}
Else return ("BOM Not Found .");
}

Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, "w ");
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}
Save the preceding code as del_bom.php, modify the directory to be checked, and run the code. This may help detect which file contains BOM information, resulting in a blank section at the beginning of all pages.

Save the following code as bom. php and remember to save it as utf8
Copy codeThe Code is as follows: <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<H3> <? Echo $ _ POST ["dir"];?> Detection results in the directory <? Php
// This file is used to quickly test UTF8 encoding. It is not added with BOM and can be automatically removed.
// By bob
// Modify Feng Yin
$ Directory = str_replace ("", "|", $ _ POST ["dir"]); // accept the submitted path data
$ Basedir = "$ directory"; // modify the directory to be checked for this behavior. The dot indicates the current directory.
$ Auto = 1; // whether to automatically remove the detected BOM. 1 is yes, 0 is 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"). "<br> ";
}
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 ("<font color = red> -- the Bom has been cleared. </Font> ");
} Else {
Return ("<font color = red> -- Bom found. </font> ");
}
}
Else return ("-- Bom not checked .");
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, "w ");
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}
?>

<Form action = "" method = "POST">
Directory: <input type = "text" name = "dir"/>
<Input type = "submit" value = "Check directory">
</Form>
Enter a folder name such as plugin/fanfou without adding /. If you want to check the root directory and enter ".", which is a decimal point submission, you can
</Br>

I hope this article will help you with PHP programming.




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.