PHP removes the bom header from the utf8 file

Source: Internet
Author: User
Tags foreach chr ord php code win32

Example 1

The code is as follows: Copy code

/**
* Remove the bom header from the file.
* @ Var 0.1
* @ Author Chenwp
*/
Function clearbom ($ contents ){
// Remove the bom header from the text by UTF8
$ BOM = chr (239). chr (187). chr (191 );
Return str_replace ($ BOM, '', $ contents );
}

/**
* Remove the bom header from the file.
* @ Param object $ fileName Description
* @ Return object Description
*/
Function clearfilebom ($ fileName ){
$ C = file_get_contents ($ fileName );
$ C = clearbom ($ c );
File_put_contents ($ fileName, $ c );
}

Example 2

How do I convert the format with BOM files into a non-signed UTF-8 format file? The following is a piece of PHP code:

The code is as follows: Copy code

<? Php

// This file is used to quickly test whether the UTF-8 encoded file is added with BOM and can be automatically removed.

$ 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 );
}
// End
?>


 

Example 3

The code is as follows: Copy code


<? Php
// Set the root directory of the BOM to be cleared (all subdirectories and files are automatically scanned)
$ HOME = dirname (_ FILE __);
// If it is a Windows system, change it to: $ WIN = 1;
$ WIN = 0;
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> UTF8 BOM cleaning tool </title>
<Style>
Body {font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: # FFF; color: #000 ;}
. FOUND {color: # F30; font-size: 14px; font-weight: bold ;}
</Style>
</Head>
<Body>
<? Php
$ BOMBED = array ();
RecursiveFolder ($ HOME );
Echo 'Foreach ($ BOMBED as $ utf) {echo $ utf. "<br/> n ";}
Echo '</p> ';
// Recursive scan
Function RecursiveFolder ($ sHOME ){
Global $ BOMBED, $ WIN;
$ Win32 = ($ WIN = 1 )? "\":"/";
$ Folder = dir ($ sHOME );
$ Foundfolders = array ();
While ($ file = $ folder-> read ()){
If ($ file! = "." And $ file! = ".."){
If (filetype ($ sHOME. $ win32. $ file) = "dir "){
$ Foundfolders [count ($ foundfolders)] = $ sHOME. $ win32. $ file;
} Else {
$ Content = file_get_contents ($ sHOME. $ win32. $ file );
$ BOM = SearchBOM ($ content );
If ($ BOM ){
$ BOMBED [count ($ BOMBED)] = $ sHOME. $ win32. $ file;
// Remove BOM information
$ Content = substr ($ content, 3 );
// Write back to the original file
File_put_contents ($ sHOME. $ win32. $ file, $ content );
    }
   }
  }
 }
$ Folder-> close ();
If (count ($ foundfolders)> 0 ){
Foreach ($ foundfolders as $ folder ){
RecursiveFolder ($ folder, $ win32 );
  }
 }
}
// Search for BOM in the current file
Function SearchBOM ($ string ){
If (substr ($ string, 0, 3) = pack ("CCC", 0xef, 0xbb, 0xbf) return true;
Return false;
}
?>
</Body>
</Html>

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.