如何删除多个UTF-8 BOM序列 [英] How to remove multiple UTF-8 BOM sequences

查看:140
本文介绍了如何删除多个UTF-8 BOM序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP5(cgi)从文件系统输出模板文件,并出现吐出原始HTML的问题.

Using PHP5 (cgi) to output template files from the filesystem and having issues spitting out raw HTML.

private function fetch($name) {
    $path = $this->j->config['template_path'] . $name . '.html';
    if (!file_exists($path)) {
        dbgerror('Could not find the template "' . $name . '" in ' . $path);
    }
    $f = fopen($path, 'r');
    $t = fread($f, filesize($path));
    fclose($f);
    if (substr($t, 0, 3) == b'\xef\xbb\xbf') {
        $t = substr($t, 3);
    }
    return $t;
}

即使我添加了BOM修复程序,但Firefox接受它仍然有问题.您可以在此处看到实时副本: http://ircb.in/jisti/(以及模板文件I如果要检查,请扔到 http://ircb.in/jisti/home.html 退出)

Even though I've added the BOM fix I'm still having problems with Firefox accepting it. You can see a live copy here: http://ircb.in/jisti/ (and the template file I threw at http://ircb.in/jisti/home.html if you want to check it out)

有什么办法解决这个问题吗? o_o

Any idea how to fix this? o_o

推荐答案

您将使用以下代码删除utf8 bom

you would use the following code to remove utf8 bom

//Remove UTF8 Bom

function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}

这篇关于如何删除多个UTF-8 BOM序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆