PHP UTF-8 file BOM Automatic Detection removal Program

Source: Internet
Author: User
Tags flock

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.

[Code] <?
// 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 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> 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 );
} [/Code] 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

 

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<H3> <? Echo $ _ post ["dir"];?> Detection results in the directory <?
// This file is used to quickly test whether the UTF-8 encoded file is 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>

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.