Last active
May 27, 2020 03:06
-
-
Save Yi-Z0/7ce5a748fdd5d8657e65fca2cdc7b1d2 to your computer and use it in GitHub Desktop.
PHP Remove UTF8 BOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
php fixBom.php /path/your/www 1 | |
*/ | |
$basedir = $argv[1]; | |
$auto = !empty($argv[2]); | |
checkdir($basedir); | |
function checkdir($basedir) | |
{ | |
if ($dh = opendir($basedir)) { | |
while (($file = readdir($dh)) !== false) { | |
if ($file != '.' && $file != '..') { | |
if (!is_dir($basedir . "/" . $file) && stripos($file,'.php')) { | |
echo "filename: $basedir/$file " . checkBOM("$basedir/$file") . " \n"; | |
} else { | |
$dirname = $basedir . "/" . $file; | |
checkdir($dirname); | |
} | |
} | |
} | |
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) { | |
file_put_contents($filename, substr($contents, 3)); | |
return ("BOM found, automatically removed.\n"); | |
} else { | |
return ("BOM found.\n"); | |
} | |
} else | |
return ("BOM Not Found.\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment