我有一个css文件,当我使用时,当我打开它时看起来很好PHP读取(将所有CSS文件合并为一个),此CSS具有以下字符:

php删除所有空格,所以在代码的中间中间随机ïï¿弄乱整个东西。正如我所提到的那样,当我在GEDIT中打开文件时,我实际上无法看到这些字符,因此我无法轻易删除它们。

我拓考了这个问题,文件编码显然有问题,这使得有意义的是,因为我已经通过ftp和 rsync ,具有一系列文本编辑器。我对角色编码并不了解,所以可以欣赏帮助。

如果它有帮助,则文件正在保存在UTF-8格式中,GEDIT不会让我以ISO-8859-15格式保存(文档包含一个或多个无法使用指定字符编码的字符编码)。我尝试用Windows和Linux线结尾保存它,但既不有帮助。

有帮助吗?

解决方案

三个单词:

字节订单标记(bom)

这是ISO-8859-1中的UTF-8 BOM的表示。您必须告诉您的编辑器不要使用BOM或使用不同的编辑器剥离它们。

自动化BOM的删除,您可以使用这一点问题

作为另一个答案,最好的php实际上是正确解释bom的,因为你可以使用 awk ,这样的:

 <?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...
  ?>
.

其他提示

php 中,您可以执行以下操作以删除所有非字符,包括所讨论的字符。

$response = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response);
.

对于Shell Access的人来说,这里有一个有点命令,用于在public_html目录中找到带有bom设置的所有文件 - 请务必将其更改为您服务器上的正确路径

代码:

grep -rl $'\xEF\xBB\xBF' /home/username/public_html
.

如果您对 vi 编辑器感到满意,请在VI中打开文件:

vi /path-to-file-name/file.php
.

并输入命令以删除BOM:

set nobomb
.

保存文件:

wq
.

bom只是一个字符序列($ ef $ bb $ bf for utf-8),所以只需使用脚本删除它们或配置编辑器,所以它没有添加。

从UTF-8 中删除BOM:

#!/usr/bin/perl
@file=<>;
$file[0] =~ s/^\xEF\xBB\xBF//;
print(@file);
.

我确信它很容易转换为php。

对我来说,这是:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
.

如果我删除此元,则再次出现。希望这有助于某人...

我不知道php,所以我不知道这是可能的,但最好的解决方案是读取文件作为utf-8而不是其他一些编码。BOM实际上是零宽度没有间断空间。这是空格,因此如果在正确的编码(UTF-8)中读取文件,则BOM将被解释为空格,并且在生成的CSS文件中将忽略它。

此外,在正确的编码中读取文件的另一个优点是您不必担心正在误解的字符。您的编辑器告诉您,您要保存的代码页将不会执行您需要的所有字符。如果PHP在不正确的编码中读取文件,那么除了BOM之外的其他字符很可能是静默误解的。到处都使用UTF-8,这些问题消失了。

可以使用

vim -e -c 'argdo set fileencoding=utf-8|set encoding=utf-8| set nobomb| wq'
.

用awk替换似乎工作,但它没有到位。

grep -rl $'\xEF\xBB\xBF' * | xargs vim -e -c 'argdo set fileencoding=utf-8|set encoding=utf-8| set nobomb| wq'

I had the same problem with the BOM appearing in some of my PHP files ().

If you use PhpStorm you can set at hotkey to remove it in Settings -> IDE Settings -> Keymap -> Main Menu - > File -> Remove BOM.

In Notepad++, choose the "Encoding" menu, then "Encode in UTF-8 without BOM". Then save.

See Stack Overflow question How to make Notepad to save text in UTF-8 without BOM?.

Open the PHP file under question, in Notepad++.

Click on Encoding at the top and change from "Encoding in UTF-8 without BOM" to just "Encoding in UTF-8". Save and overwrite the file on your server.

Same problem, different solution.

One line in the PHP file was printing out XML headers (which use the same begin/end tags as PHP). Looks like the code within these tags set the encoding, and was executed within PHP which resulted in the strange characters. Either way here's the solution:

# Original
$xml_string = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";

# fixed
$xml_string = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\"?" . ">";

If you need to be able to remove the BOM from UTF-8 encoded files, you first need to get hold of an editor that is aware of them.

I personally use E Text Editor.

In the bottom right, there are options for character encoding, including the BOM tag. Load your file, deselect Byte Order Marker if it is selected, resave, and it should be done.

Alt text http://oth4.com/encoding.png

E is not free, but there is a free trial, and it is an excellent editor (limited TextMate compatibility).

You can open it by PhpStorm and right-click on your file and click on Remove BOM...

Here is another good solution for the problem with BOM. These are two VBScript (.vbs) scripts.

One for finding the BOM in a file and one for KILLING the damned BOM in the file. It works pretty fine and is easy to use.

Just create a .vbs file, and paste the following code in it.

You can use the VBScript script simply by dragging and dropping the suspicious file onto the .vbs file. It will tell you if there is a BOM or not.

' Heiko Jendreck - personal helpdesk & webdesign
' http://www.phw-jendreck.de
' 2010.05.10 Vers 1.0
'
' find_BOM.vbs
' ====================
' Kleines Hilfsmittel, welches das BOM finden soll
'
 Const UTF8_BOM = ""
 Const UTF16BE_BOM = "þÿ"
 Const UTF16LE_BOM = "ÿþ"
 Const ForReading = 1
 Const ForWriting = 2
 Dim fso
 Set fso = WScript.CreateObject("Scripting.FileSystemObject")
 Dim f
 f = WScript.Arguments.Item(0)
 Dim t
 t = fso.OpenTextFile(f, ForReading).ReadAll
 If Left(t, 3) = UTF8_BOM Then
     MsgBox "UTF-8-BOM detected!"
 ElseIf Left(t, 2) = UTF16BE_BOM Then
     MsgBox "UTF-16-BOM (Big Endian) detected!"
 ElseIf Left(t, 2) = UTF16LE_BOM Then
     MsgBox "UTF-16-BOM (Little Endian) detected!"
 Else
     MsgBox "No BOM detected!"
 End If

If it tells you there is BOM, go and create the second .vbs file with the following code and drag the suspicios file onto the .vbs file.

' Heiko Jendreck - personal helpdesk & webdesign
' http://www.phw-jendreck.de
' 2010.05.10 Vers 1.0
'
' kill_BOM.vbs
' ====================
' Kleines Hilfmittel, welches das gefundene BOM löschen soll
'
Const UTF8_BOM = ""
Const ForReading = 1
Const ForWriting = 2
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim f
f = WScript.Arguments.Item(0)
Dim t
t = fso.OpenTextFile(f, ForReading).ReadAll
If Left(t, 3) = UTF8_BOM Then
    fso.OpenTextFile(f, ForWriting).Write (Mid(t, 4))
    MsgBox "BOM gelöscht!"
Else
    MsgBox "Kein UTF-8-BOM vorhanden!"
End If

The code is from Heiko Jendreck.

In PHPStorm, for multiple files and BOM not necessarily at the beginning of the file, you can search \x{FEFF} (Regular Expression) and replace with nothing.

Same problem, but it only affected one file so I just created a blank file, copy/pasted the code from the original file to the new file, and then replaced the original file. Not fancy but it worked.

Use Total Commander to search for all BOMed files:

Elegant way to search for UTF-8 files with BOM?

  • Open these files in some proper editor (that recognizes BOM) like Eclipse.

  • Change the file's encoding to ISO (right click, properties).

  • Cut  from the beginning of the file, save

  • Change the file's encoding back to UTF-8

...and do not even think about using n...d again!

I had the same problem. The problem was because one of my php files was in utf-8 (the most important, the configuaration file which is included in all php files).

In my case, I had 2 different solutions which worked for me :

First, I changed the Apache Configuration by using AddDefaultCharsetDirective in configuration files (or in .htaccess). This solution forces Apache to use the correct encodage.

AddDefaultCharset ISO-8859-1

The second solution was to change the bad encoding of the php file.

  1. Copy the text of your filename.css file.
  2. Close your css file.
  3. Rename it filename2.css to avoid a filename clash.
  4. In MS Notepad or Wordpad, create a new file.
  5. Paste the text into it.
  6. Save it as filename.css, selecting UTF-8 from the encoding options.
  7. Upload filename.css.

Check on your index.php, find "... charset=iso-8859-1" and replace it with "... charset=utf-8".

Maybe it'll work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top