如何从文件开头删除 ? [英] How do I remove  from the beginning of a file?
问题描述
我有一个 CSS 文件,当我使用 gedit 打开它时看起来不错,但是当它由 PHP 读取(将所有 CSS 文件合并为一个),此 CSS 具有以下字符:
I have a CSS file that looks fine when I open it using gedit, but when it's read by PHP (to merge all the CSS files into one), this CSS has the following characters prepended to it: 
PHP 删除了所有空格,因此代码中间的随机  会弄乱整个内容.正如我提到的,当我在 gedit 中打开文件时,我实际上看不到这些字符,所以我不能很容易地删除它们.
PHP removes all whitespace, so a random  in the middle of the code messes up the entire thing. As I mentioned, I can't actually see these characters when I open the file in gedit, so I can't remove them very easily.
我用谷歌搜索了这个问题,文件编码显然有问题,这是有道理的,因为我一直在通过 ftp 和 rsync,带有一系列文本编辑器.不过,我对字符编码不太了解,因此不胜感激.
I googled the problem, and there is clearly something wrong with the file encoding, which makes sense being as I've been shifting the files around to different Linux/Windows servers via ftp and rsync, with a range of text editors. I don't really know much about character encoding though, so help would be appreciated.
如果有帮助,文件正在以 UTF-8 格式保存,gedit 不会让我以 ISO-8859-15 格式保存它(文档包含一个或多个无法使用指定字符编码的字符)编码).我尝试使用 Windows 和 Linux 行结尾保存它,但都没有帮助.
If it helps, the file is being saved in UTF-8 format, and gedit won't let me save it in ISO-8859-15 format (the document contains one or more characters that cannot be encoded using the specified character encoding). I tried saving it with Windows and Linux line endings, but neither helped.
推荐答案
给你三个字:
这是 ISO-8859-1 中 UTF-8 BOM 的表示.您必须告诉您的编辑不要使用 BOM 或使用不同的编辑器将它们删除.
That's the representation for the UTF-8 BOM in ISO-8859-1. You have to tell your editor to not use BOMs or use a different editor to strip them out.
要自动移除 BOM,您可以使用 awk
,如 这个问题.
To automatize the BOM's removal you can use awk
as shown in this question.
作为 另一个答案说,最好让 PHP 正确解释 BOM,为此您可以使用 mb_internal_encoding()
,像这样:
As another answer says, the best would be for PHP to actually interpret the BOM correctly, for that you can use mb_internal_encoding()
, like this:
<?php
//Storing the previous encoding in case you have some other piece
//of code sensitive to encoding and counting on the default value.
$previous_encoding = mb_internal_encoding();
//Set the encoding to UTF-8, so when reading files it ignores the BOM
mb_internal_encoding('UTF-8');
//Process the CSS files...
//Finally, return to the previous encoding
mb_internal_encoding($previous_encoding);
//Rest of the code...
?>
这篇关于如何从文件开头删除 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!