Last active
July 21, 2016 21:10
-
-
Save max-dark/01d2bc7c54fa2f161d54add970763807 to your computer and use it in GitHub Desktop.
Утилита для пакетного удаления 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 | |
/** | |
* bumbom.php | |
* @link https://gist.github.com/max-dark/01d2bc7c54fa2f161d54add970763807 | |
* | |
* Утилита для пакетного удаления BOM | |
* Использование: | |
* 0 создать бекап всех файлов | |
* 1 Закинуть в папку с файлами | |
* 2 поправить(если надо) $ext_list | |
* 3 запустить: php bumbom.php | |
* | |
* Распространяется "как есть". Использование на свой страх и риск и так далее... | |
* | |
*/ | |
$ext_list = implode('|', [ 'php','js','css','htm', 'htaccess']); | |
const U8_BOM = "\xEF\xBB\xBF"; | |
const U16_BE = "\xFE\xFF"; | |
const U16_LE = "\xFF\xFE"; | |
function bumbom($dir, $ext_list) { | |
$stack = [$dir]; | |
while (!empty($stack)) { | |
$current = array_pop($stack); | |
$d = opendir($current); | |
while ($filename = readdir($d)) { | |
if (in_array($filename, ['.', '..'])) continue; | |
$filename = $current . DIRECTORY_SEPARATOR .$filename; | |
if (is_dir($filename)) { | |
array_push($stack, $filename); | |
continue; | |
} | |
if (preg_match("~\\.($ext_list)\$~", $filename)) { | |
$content = file_get_contents($filename); | |
$have_bom = (substr($content,0,3) === U8_BOM); | |
if ($have_bom) { | |
echo ' BOM removed from '; | |
file_put_contents($filename,substr($content, 3)); | |
echo $filename.PHP_EOL; | |
} | |
} | |
} | |
} | |
} | |
bumbom(__DIR__, $ext_list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment