PHP remove BOM header in UTF8 format file _php tutorial

Source: Internet
Author: User
We sometimes have to get rid of the UTF8 in the document head we often need to remove the manual, below I have compiled a few PHP programs to clear the UTF8 format files in the BOM head method, I hope to help you.

Example 1

The code is as follows Copy Code

/**
* Remove BOM header from the file
* @var 0.1
* @author CHENWP
*/
function Clearbom ($contents) {
UTF8 Remove BOM Header from text
$BOM = Chr (239). chr (187). CHR (191);
Return Str_replace ($BOM, ", $contents);
}

/**
* Remove 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 a format with a BOM file to a UTF-8 format file without a signature? Let's share the following PHP code:

The code is as follows Copy Code

This file is used to quickly test whether UTF8 encoded files are added to the BOM and can be automatically removed

$basedir = "."; Modify this behavior to detect the directory where the point represents the current directory
$auto = 1; Whether the discovered BOM information is automatically removed. 1 for Yes, 0 for 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 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);
}
End
?>


Example 3

The code is as follows Copy Code


Set the root directory where you want to clear the BOM (all subdirectories and files will be scanned automatically)
$HOME = DirName (__file__);
If it is a Windows system, modify it to: $WIN = 1;
$WIN = 0;
?>




UTF8 BOM Cleaner



$BOMBED = Array ();
Recursivefolder ($HOME);
Echo '

These files had UTF8 BOM, but I cleaned them:

';
foreach ($BOMBED as $utf) {echo $utf.
n "; }
Echo '

';
Recursive scan
function Recursivefolder ($sHOME) {
Global $BOMBED, $WIN;
$win = ($WIN = = 1)? "\" : "/";
$folder = Dir ($sHOME);
$foundfolders = Array ();
while ($file = $folder->read ()) {
if ($file! = "." and $file! = "...") {
if (filetype ($sHOME. $win. $file) = = "Dir") {
$foundfolders [Count ($foundfolders)] = $sHOME. $win 32. $file;
} else {
$content = file_get_contents ($sHOME. $win. $file);
$BOM = Searchbom ($content);
if ($BOM) {
$BOMBED [Count ($BOMBED)] = $sHOME. $win 32. $file;
Move out of BOM information
$content = substr ($content, 3);
Write back to the original file
File_put_contents ($sHOME. $win. $file, $content);
}
}
}
}
$folder->close ();
if (count ($foundfolders) > 0) {
foreach ($foundfolders as $folder) {
Recursivefolder ($folder, $win 32);
}
}
}
Search the current file for a BOM
function Searchbom ($string) {
if (substr ($string, 0,3) = = Pack ("CCC", 0XEF,0XBB,0XBF)) return true;
return false;
}
?>

http://www.bkjia.com/PHPjc/632745.html www.bkjia.com true http://www.bkjia.com/PHPjc/632745.html techarticle we sometimes have to get rid of the UTF8 in the document head we often need to manually clear, below I cleaned up a few PHP programs to clear the UTF8 format files in the BOM head method, I hope you have the same ...

  • Related Article

    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.