Skip to content

Instantly share code, notes, and snippets.

@Yi-Z0
Last active May 27, 2020 03:06
Show Gist options
  • Save Yi-Z0/7ce5a748fdd5d8657e65fca2cdc7b1d2 to your computer and use it in GitHub Desktop.
Save Yi-Z0/7ce5a748fdd5d8657e65fca2cdc7b1d2 to your computer and use it in GitHub Desktop.
PHP Remove UTF8 BOM
<?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