如何从文件的开头删除 [英] 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格式(文档包含一个或多个字符,不能使用指定的字符编码进行编码)。
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的表示形式。
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屋!