WP主题奇怪的问题。我index是写<meta charset="UTF-8" />的,但是主页php文件用UTF-8就会把界面读得乱起七八招,非得用UTF-8 without BOM。

2011-03-21 04:28:38 +08:00
 SolidZORO
WP主题奇怪的问题。我index是写<meta charset="UTF-8" />的,但是主页php文件用UTF-8就会把界面读得乱起七八招,非得用UTF-8 without BOM。怎么回事嘛?这个很可恶嘢。我做别的主题都不会啊,害我debug一个通宵。
6315 次点击
所在节点    问与答
6 条回复
SolidZORO
2011-03-21 04:37:56 +08:00
你知道我是怎么解决的么?我先用notepad++保存成UTF-8 without BOM,WP主题就正常了,切到UTF-8还是会错位。然后我用windows的记事本打开这个UTF-8 without BOM的php 另存为 UTF-8的php。这样就解决了问题。

所以我是搞不懂了。notepad++出来的UTF-8真的不标准么?难道我还真的撞倒bug了哦? 好诡异的事情阏~
MiniLight
2011-03-21 05:43:14 +08:00
BOM 标记会带来很多未知问题 大名鼎鼎的“锟斤拷”就出自于此 是不被推荐使用的 这段代码放在服务器上可以帮助你去除PHP文件中隐藏的 BOM 标记

<?php
//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);
}
?>
est
2011-03-21 08:54:31 +08:00
呃其实 锟斤拷 和bom无关,只是unicode站位符
no2x
2011-03-21 10:19:14 +08:00
估计是有其他文件带了 BOM 吧,否则不会这样。

我倒是试过其他文件没带 BOM 的时候,一个模板带了 BOM 导致顶头多了行空白。
SolidZORO
2011-03-21 13:39:16 +08:00
@no2x 我的物难题就是顶头空白。其问题是<head></head>里面完全没有东西。 跑到<body>里面去了。如果是这样。

问题是别的模版是UTF-8的。为什么都行。就那个模版不行? 这很奇怪把? 其实UTF-8 without BOM是ANSI系的东西。只是多了几个byte。

@MiniLight 这个代码实在好。我得好好收藏一下。谢谢。
freefcw
2011-03-21 13:53:33 +08:00
BOM这个东西的问题确实是很多的,文件还是不要比较好,特别是写网页脚本

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/10124

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX