V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
SolidZORO
V2EX  ›  问与答

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

  •  
  •   SolidZORO · 2011-03-21 04:28:38 +08:00 · 6301 次点击
    这是一个创建于 4807 天前的主题,其中的信息可能已经有所发展或是发生改变。
    WP主题奇怪的问题。我index是写<meta charset="UTF-8" />的,但是主页php文件用UTF-8就会把界面读得乱起七八招,非得用UTF-8 without BOM。怎么回事嘛?这个很可恶嘢。我做别的主题都不会啊,害我debug一个通宵。
    6 条回复    1970-01-01 08:00:00 +08:00
    SolidZORO
        1
    SolidZORO  
    OP
       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
        2
    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
        3
    est  
       2011-03-21 08:54:31 +08:00
    呃其实 锟斤拷 和bom无关,只是unicode站位符
    no2x
        4
    no2x  
       2011-03-21 10:19:14 +08:00
    估计是有其他文件带了 BOM 吧,否则不会这样。

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

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

    @MiniLight 这个代码实在好。我得好好收藏一下。谢谢。
    freefcw
        6
    freefcw  
       2011-03-21 13:53:33 +08:00
    BOM这个东西的问题确实是很多的,文件还是不要比较好,特别是写网页脚本
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2315 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:15 · PVG 13:15 · LAX 22:15 · JFK 01:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.