小编典典

如何从文件开头删除

php

我有一个CSS文件,当我使用gedit打开它时看起来不错,但是当PHP读取它(将所有CSS文件合并为一个文件)时,该CSS前面有以下字符:

PHP删除了所有空格,因此代码中间的一个随机“ 正如我提到的,当我在gedit中打开文件时,我实际上看不到这些字符,因此我无法轻松地删除它们。

我用谷歌搜索了这个问题,文件编码显然有问题,这是有道理的,因为我一直在通过ftp和rsync使用文件编辑器将文件转移到不同的Linux
/ Windows服务器上。我对字符编码的了解并不多,所以可以提供帮助。

如果有帮助,该文件将以UTF-8格式保存,而gedit不允许我将其保存为ISO-8859-15格式(文档包含一个或多个无法使用指定字符编码进行编码的字符)。我尝试将其与Windows和Linux行尾一起保存,但均无济于事。


阅读 642

收藏
2020-05-26

共1个答案

小编典典

三个字给您:

字节顺序标记(BOM)

这就是ISO-8859-1中UTF-8 BOM的表示形式。您必须告诉编辑器不要使用BOM表,或使用其他编辑器将它们删除。

另一个答案所说的那样最好的办法是让PHP正确地正确解释BOM,因为您可以使用mb_internal_encoding(),例如:

 <?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...
  ?>
2020-05-26