Гуру | например можно создать php файл в utf=8 без bom и назвать его find_bom.php с таким кодом:
Развернуть текст PHP код: <?
function chechBom($path,$recurs) {
global $find;
global $arr;
if ($dir = @opendir($path)) {
while($file = readdir($dir)) {
if ($file == '.' or $file == '..') continue;
$file = $path.'/'.$file;
if (is_dir($file) && $recurs) {
chechBom($file,1);
}
if (is_file($file) && strstr($file,'.php')) {
$f = fopen($file,'r');
$t = fread($f, 3);
if ($t == "\xEF\xBB\xBF") {
$GLOBALS['arr']['bom'][] = $file;
} fclose ($f);
$n = file_get_contents($file);
if(!is_utf8($n))$GLOBALS['arr']['utf'][] = $file;
}
}
closedir($dir);
}
}
$char_re = ' [\x09\x0A\x0D\x20-\x7E] # ASCII strict
# [\x00-\x7F] # ASCII non-strict (including control chars)
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
';
function is_utf8($data, $is_strict = true) {
if (is_array($data)) {
foreach ($data as $k => &$v) {
if (! is_utf8($k, $is_strict) || ! is_utf8($v, $is_strict)) return false;
} return true;
}
if (is_string($data)) {
if ($data === '') return true;
if ($is_strict && preg_match('/[^\x09\x0A\x0D\x20-\xBF\xC2-\xF7]/sSX', $data)) return false;
if (function_exists('mb_check_encoding')) return mb_check_encoding($data, 'UTF-8');
if (function_exists('iconv')) return @iconv('UTF-8', 'UTF-16', $data) !== false;
$result = $is_strict ? preg_replace('/(?>' . $char_re . '
#| (.) # catch bad bytes
)+/sxSX', '', $data)
:
preg_replace('/\X+/suSX', '', $data);
if (function_exists('preg_last_error')) {
if (preg_last_error() === PREG_NO_ERROR) return $result === '';
if (preg_last_error() === PREG_BAD_UTF8_ERROR) return false;
} elseif (is_string($result)) return $result === '';
return check($data, $is_strict);
}
if (is_scalar($data) || is_null($data)) return true;
return false;
}
function check($s, $is_strict = true) {
for ($i = 0, $len = strlen($s); $i < $len; $i++) {
$c = ord($s[$i]);
if ($c < 0x80) {
if ($is_strict === false || ($c > 0x1F && $c < 0x7F) || $c == 0x09 || $c == 0x0A || $c == 0x0D) continue;
}
if (($c & 0xE0) == 0xC0) $n = 1;
elseif (($c & 0xF0) == 0xE0) $n = 2;
elseif (($c & 0xF8) == 0xF0) $n = 3;
elseif (($c & 0xFC) == 0xF8) $n = 4;
elseif (($c & 0xFE) == 0xFC) $n = 5;
else return false;
for ($j = 0; $j < $n; $j++) {
$i++;
if ($i == $len || ((ord($s[$i]) & 0xC0) != 0x80) ) return false;
}
} return true;
}
$GLOBALS['arr'] = array();
chechBom('.',0);
chechBom('./a',0);
chechBom('./admin',0);
chechBom('./core',1);
chechBom('./inc',1);
if(@count($GLOBALS['arr']['bom'])){
foreach($GLOBALS['arr']['bom'] as $v) echo "<strong>Наличие BOM:</strong> ".$v."<br />";
}else echo "<strong>Файлов с BOM не найдено</strong><br />---------------------------------<br />";
if(@count($GLOBALS['arr']['utf'])){
foreach($GLOBALS['arr']['utf'] as $v) echo "<strong>Не UTF8:</strong> ".$v."<br />";
}else echo "<strong>Файлов не в UTF8 не найдено</strong><br />---------------------------------<br />";
?>
[свернуть] залить в корень сайта и запустить по адресу http://site.ru/find_bom.php и он покажет какие файлы в неправильной кодировке |
|