PHP 会输出空白隐形字符 ,这样一来就会导致页面出现空白一行的情况,具体情况见下图:
这种因为  导致页面空白一行的解决办法也很简单,只需要找到相应的 PHP 文件,然后用编辑器打开该文件然后另存为 UTF-8 无 BOM 格式即可。如 Notepad++ 软件,打开该文件后,点击菜单栏的编码 – 选择“以 UTF-8 无 BOM 格式编码”,然后保存即可。
PS:window 的记事本保存的文件都是带 BOM 的,所以编辑 PHP 文件不建议使用记事本,切记!!!
当然,你也可以将以下代码另存为 PHP 文件,上传到站点根目录下运行,就会把目录下全部 utf-8 文件的 bom 头去掉。
- //remove the utf-8 boms
- //by magicbug at gmail dot com
- 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) {
- 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);
- }