用PHP解决万恶的BOM - 主打产品 - 南充市顺庆区小房子和树婚礼策划部
当前位置:首页 > 主打产品 >

用PHP解决万恶的BOM

编辑:南充市顺庆区小房子和树婚礼策划部时间:2017-09-09 10:17:57阅读次数:2
用PHP解决万恶的BOM

[php]
<?php
$path = dirname(__FILE__);

scandDir($path);


function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;

if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);

if (strlen($headers) < 3) return;

if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);

if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}


}

function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");

while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, "\n";
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}

<?php
$path = dirname(__FILE__);

scandDir($path);


function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;

if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);

if (strlen($headers) < 3) return;

if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);

if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}


}

function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网络推广 https://www.feimao666.com

上一篇:SEOER必修课:浅析如何编辑出一篇成功的软文 下一篇:最后一页

相关阅读