PHP realize UTF-8 file BOM automatic detection and removal instance, _ PHP Tutorial

Source: Internet
Author: User
PHP realize UTF-8 file BOM automatic detection and removal instance ,. PHP to realize the UTF-8 file BOM automatic detection and removal instance, this article describes the example of PHP to realize the UTF-8 file BOM automatic detection and removal method. Share it with you for your reference. Specific Implementation methods PHP realize UTF-8 file BOM automatic detection and removal of instances,

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.

The 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 ")."
";
}
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 );
}


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

The code is as follows:


<? Echo $ _ POST ["dir"];?> Directory detection results
<? 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 ")."
";
}
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 has been cleared. ");
} Else {
Return ("-- Bom found .");
}
}
Else return ("-- Bom not checked .");
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, "w ");
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}
?>


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

I hope this article will help you with PHP programming.




Examples, this article describes the PHP implementation of UTF-8 file BOM automatic detection and removal methods. Share it with you for your reference. Implementation method...

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.