The reason top of the site page appears blank line & # 65279 characters as well as perfect solutions

Transfer from personal blog : http://hurbai.com

Sometimes the head of the page there will be a blank line to view the source code found in the beginning of the beginning of the body there is an illegal character &#65279, because the page encoding is UTF-8 + BOM.

UTF-8 + BOM encoding typically appear in the windows operating system such as WINDOWS notepad software, in order to save the time of a UTF-8 encoded file, not visible in the three inserted where the start of the file character (0xEF 0xBB 0xBF, namely BOM). It is a string of hidden characters, for letting editor such as Notepad to identify whether the file in UTF-8 encoding. For the average file, this will not produce any trouble. But for PHP is, BOM is a big trouble. Because PHP does not ignore the BOM, so when reading, or contains references to these documents, as part of the BOM will begin with the text of the document. According to the characteristics of embedded languages, this string of characters will be executed directly (display) up, that we see (& # 65279) characters.

Solution

Find appear &#65279relevant page of the character (php, html, css, js, etc.), see page encoding, if it is UTF-8 + BOM encoding, use notepad ++ or other tools stored as "UTF-8 without BOM" can be solved .

If the file is more, when I do not know where to start, then use the following methods to achieve.

Save the following code a.php(any name) file into the root directory, and then run this file, you can automatically clean storage format.

** NOTE: ** If it is to achieve clear in the server, for safety reasons, first back up again under operation Also, please ensure that the file has write permissions, otherwise it can not be removed.

<?php
// 设定你要清除BOM的根目录(会自动扫描所有子目录和文件)
$HOME = dirname(__FILE__);
// 如果是Windows系统,修改为:$WIN = 1;
$WIN = 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>UTF8 BOM 清除器</title>
    <style>
        body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; }
        .FOUND { color: #F30; font-size: 14px; font-weight: bold; }
    </style>
</head>
<body>
<?php
$BOMBED = array();
RecursiveFolder($HOME);
echo '<h2>这些文件有UTF8 BOM,但我清理了它们::</h2><p class="FOUND">';
foreach ($BOMBED as $utf) { echo $utf ."<br />\n"; }
echo '</p>';
// 递归扫描
function RecursiveFolder($sHOME) {
    global $BOMBED, $WIN;
    $win32 = ($WIN == 1) ? "\\" : "/";
    $folder = dir($sHOME);
    $foundfolders = array();
    while ($file = $folder->read()) {
        if($file != "." and $file != "..") {
            if(filetype($sHOME . $win32 . $file) == "dir"){
                $foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
            } else {
                $content = file_get_contents($sHOME . $win32 . $file);
                $BOM = SearchBOM($content);
                if ($BOM) {
                    $BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
                    // 移出BOM信息
                    $content = substr($content,3);
                    // 写回到原始文件
                    file_put_contents($sHOME . $win32 . $file, $content);
                }
            }
        }
    }
    $folder->close();
    if(count($foundfolders) > 0) {
        foreach ($foundfolders as $folder) {
            RecursiveFolder($folder, $win32);
        }
    }
}
// 搜索当前文件是否有BOM
function SearchBOM($string) {
    if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
    return false;
}
?>
</body>
</html>

Transfer from personal blog : http://hurbai.com

Guess you like

Origin www.cnblogs.com/hurbai/p/11288554.html