Hello! It's a great pleasure to have you visit my blog.
It's truly a fateful encounter. If you are interested in Chinese goods, we can collaborate.
I can provide you with affordable Chinese products that you can sell in your country, thereby boosting our economic income.


If you are willing to cooperate, I hope to hear from you to discuss the opportunity for collaboration.
Here is my Email: t@29ym.com
Looking forward to connecting with you and embarking on this partnership together!

PHP一键去除网页BOM属性

日常开发或者迁移服务器经常遇到几个常出现的问题:
1.网站打开空白
2.页面头部出现多余的空白,出现&#65279代码
解决方法可以是:
1.选用专业的编辑器,例如notepad++,sublime,editplus这样不会自动签名。
2.sublime通过如下操作File -> Save with Encoding -> UTF-8保存后即可去除bom
3.notepad++选中格式 -> 以UTF-8格式编码 选项即可去除
也可以用以下PHP代码,放在网站根目录运行,可以去掉网站所有的BOM。

if (isset ( $_GET ['dir'] )) { // config the basedir
    $basedir = $_GET ['dir'];
} else {
    $basedir = '.';
}
$auto = 1;
checkdir ( $basedir );
function checkdir($basedir) {
    if ($dh = opendir ( $basedir )) {
        while ( ($file = readdir ( $dh )) !== false ) {
            if ($file != '.' && $file != '..') {
                if (! is_dir ( $basedir . "/" . $file )) { // 如果是文件
                    echo "filename: $basedir/$file " . checkBOM ( "$basedir/$file" ) . " <br>";
                } 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) { // BOM
                                                                                                     // 的前三个字符的ASCII
                                                                                                     // 码分别为
                                                                                                     // 239
                                                                                                     // 187
                                                                                                     // 191
        if ($auto == 1) {
            $rest = substr ( $contents, 3 );
            rewrite ( $filename, $rest );
            return ("<font color=red>BOM found, automatically removed.</font>");
        } else {
            return ("<font color=red>BOM found.</font>");
        }
    } else
        return ("BOM Not Found.");
}
function rewrite($filename, $data) {
    $filenum = fopen ( $filename, "w" );
    flock ( $filenum, LOCK_EX );
    fwrite ( $filenum, $data );
    fclose ( $filenum );
}
仅有 1 条评论
  1. 过客

    对于这种情况没有必要放到网站上面,直接用 php 的 cli 模式就好了。

    过客 December 4th, 2018 at 01:28 pm回复
发表新评论