The most awesome file in history. BOM Header cleanup code, universal detection clear PHP,JS, etc.

Source: Internet
Author: User

PHP development, often part of the uft8 PHP file due to different BOM header causes a variety of errors and dislocation. Most of the so-called whereabouts of the online BOM tools are 2 goods, not available. The following section of code, please copy to a PHP file, and then upload to your site root directory, and then the front desk access, he will automatically the site under all the directories and subdirectories of the various documents of the BOM, will automatically judge Yo.

<?php
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;
?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>utf8 BOM Cleaner </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>
<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;
$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;
}
?>
</body>

The most awesome file in history. BOM Header cleanup code, universal detection clear PHP,JS, etc.

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.