wordpress CMS主题 微信
Home > 网站建设 > After using the UTF-8 page why not display center?

After using the UTF-8 page why not display center?

高时银博客 网站建设 点击: 1,869 次 0 0

After using the UTF-8 page why not display center? Use WordPress to doTaobao guest website, use Notepad to save a page to UTF-8 format, the entire page is not centered, but left the show. Don't always know what is going on, after many research, finally found: output the entire page may have BOM character.

BOM -- Byte Order Mark, Chinese name translated as "byte order mark". To find a note on BOM here:

In the UCS code is called a "Zero Width No-Break Space", Chinese translationmasterpieces "zero width no interruption interval" character, it is encoded in FEFF. While the FFFE in UCS is not the existence of characters, so should not appear in the actual transmission. The UCS specification suggest transmissionbyte stream, the first transmission character "Zero Width No-Break Space". So if the receiver receives the FEFF, the byte stream is Big-Endian; if FFFE, indicates the byte stream is Little- Endian. So the character "Zero Width No-Break Space"("zero width no break space") also known as BOM.

UTF-8 does not require the BOM to show the byte order, but can show thecoding with BOM. The UTF-8 character code "Zero Width No-Break Space" is EFBB BF. So if the recipient received by EF BB BF at the beginning of the byte stream, know that it is the UTF-8 code. Windows is marked with the BOM text fileencoding.

To the Internet to find a small program to detect BOM, this PHP procedure is as follows, to save the PHP file in the root directory of a line (file name casually), will automatically remove all BOM in the file header. It is as expected, as expected,BOM is in trouble, clearance of BOM after good. This code online to find really good, not only can detect BOM, can automatically clear BOM, code is as follows:

$basedir = '.';
$auto = 1;
function checkdir($basedir){
if($dh = opendir($basedir)){
while(($file = readdir($dh)) !== false){
if($file != '.' && $file != '..'){
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
convertN("$basedir/$file"); //加个LINUX换行
$dirname = $basedir."/".$file;
}//end while
}//end if($dh
}//end function
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>";
return ("<font color=red>BOM found.</font>");
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);

function convertN($file) {
$str = file_get_contents($file);
$str = str_replace("\r\n", "\n", $str);
file_put_contents($file, $str, LOCK_EX);

The above code is saved as an PHP file, folder root directory into the you want to remove the BOM file, run them again. Because you have this problem, so, write out the hope can help also encountered this problem friends.



版权所有 © 转载时必须以链接形式注明作者和原始出处!