she*_*dzw 50 php byte-order-mark utf-8
使用PHP5(cgi)从文件系统输出模板文件,并且有问题吐出原始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;
}
Run Code Online (Sandbox Code Playgroud)
即使我已经添加了BOM修复程序,我仍然遇到Firefox接受它的问题.你可以在这里看到一个实时的副本:http://ircb.in/jisti/(如果你想查看它,我在http://ircb.in/jisti/home.html投掷的模板文件)
知道如何解决这个问题吗?O_O
小智 133
你将使用以下代码删除utf8 bom
//Remove UTF8 Bom
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
Run Code Online (Sandbox Code Playgroud)
小智 34
尝试:
// -------- read the file-content ----
$str = file_get_contents($source_file);
// -------- remove the utf-8 BOM ----
$str = str_replace("\xEF\xBB\xBF",'',$str);
// -------- get the Object from JSON ----
$obj = json_decode($str);
Run Code Online (Sandbox Code Playgroud)
:)
Dea*_* Or 13
另一种删除BOM的方法是Unicode代码点U + FEFF
$str = preg_replace('/\x{FEFF}/u', '', $file);
Run Code Online (Sandbox Code Playgroud)
b'\xef\xbb\xbf'
代表文字字符串"\ xef\xbb\xbf".如果要检查BOM,则需要使用双引号,因此\x
序列实际上被解释为字节:
"\xef\xbb\xbf"
Run Code Online (Sandbox Code Playgroud)
您的文件似乎还包含比单个前导BOM更多的垃圾:
$ curl http://ircb.in/jisti/ | xxd
0000000: efbb bfef bbbf efbb bfef bbbf efbb bfef ................
0000010: bbbf efbb bf3c 2144 4f43 5459 5045 2068 .....<!DOCTYPE h
0000020: 746d 6c3e 0a3c 6874 6d6c 3e0a 3c68 6561 tml>.<html>.<hea
...
Run Code Online (Sandbox Code Playgroud)
小智 6
如果有人使用 csv import 那么下面的代码很有用
$header = fgetcsv($handle);
foreach($header as $key=> $val) {
$bom = pack('H*','EFBBBF');
$val = preg_replace("/^$bom/", '', $val);
$header[$key] = $val;
}
Run Code Online (Sandbox Code Playgroud)
该全局函数解析 UTF-8 系统基本字符集。坦克!
function prepareCharset($str) {
// set default encode
mb_internal_encoding('UTF-8');
// pre filter
if (empty($str)) {
return $str;
}
// get charset
$charset = mb_detect_encoding($str, array('ISO-8859-1', 'UTF-8', 'ASCII'));
if (stristr($charset, 'utf') || stristr($charset, 'iso')) {
$str = iconv('ISO-8859-1', 'UTF-8//TRANSLIT', utf8_decode($str));
} else {
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
}
// remove BOM
$str = urldecode(str_replace("%C2%81", '', urlencode($str)));
// prepare string
return $str;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
65415 次 |
最近记录: |