如何删除 从文件的开头?

发布于 2024-09-10 08:36:40 字数 606 浏览 13 评论 0原文

我有一个 CSS 文件,当我使用 gedit 打开它时,它看起来很好,但是当它被读取时PHP(将所有 CSS 文件合并为一个),此 CSS 前面添加了以下字符: 

PHP 会删除所有空格,因此随机 代码中间搞乱了整个事情。正如我提到的,当我在 gedit 中打开文件时,我实际上看不到这些字符,所以我不能很容易地删除它们。

我用谷歌搜索了这个问题,文件编码显然有问题,这是有道理的,因为我一直通过 ftp 和 rsync,具有一系列文本编辑器。虽然我对字符编码不太了解,所以我们将不胜感激。

如果有帮助,该文件将以 UTF-8 格式保存,并且 gedit 不允许我以 ISO-8859-15 格式保存它(文档包含一个或多个无法使用指定字符编码进行编码的字符)。我尝试使用 Windows 和 Linux 行结尾来保存它,但都没有帮助。

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

残龙傲雪 2024-09-17 08:36:40

给您三个词:

字节顺序标记 (BOM)

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

要自动删除 BOM,您可以使用 awk,如 这个问题

正如另一个答案所说,最好的办法是 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...
  ?>

Three words for you:

Byte Order Mark (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.

To automatize the BOM's removal you can use awk as shown in this question.

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...
  ?>
握住你手 2024-09-17 08:36:40

Notepad++ 中打开文件。从编码菜单中,选择转换为无BOM的UTF-8,保存文件,用此新文件替换旧文件。它一定会起作用的。

Open your file in Notepad++. From the Encoding menu, select Convert to UTF-8 without BOM, save the file, replace the old file with this new file. And it will work, damn sure.

人生戏 2024-09-17 08:36:40

PHP 中,您可以执行以下操作来删除所有非字符,包括相关字符。

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

In PHP, you can do the following to remove all non characters including the character in question.

$response = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response);
橘虞初梦 2024-09-17 08:36:40

对于那些具有 shell 访问权限的人来说,这里有一个小命令,用于查找 public_html 目录中设置了 BOM 的所有文件 - 请务必将其更改为服务器上的正确路径代码

grep -rl 

如果您对 vi编辑器,在vi中打开文件:

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

并输入删除BOM的命令:

set nobomb

保存文件:

wq
\xEF\xBB\xBF' /home/username/public_html

如果您对 vi编辑器,在vi中打开文件:

并输入删除BOM的命令:

保存文件:

For those with shell access here is a little command to find all files with the BOM set in the public_html directory - be sure to change it to what your correct path on your server is

Code:

grep -rl 

and if you are comfortable with the vi editor, open the file in vi:

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

And enter the command to remove the BOM:

set nobomb

Save the file:

wq
\xEF\xBB\xBF' /home/username/public_html

and if you are comfortable with the vi editor, open the file in vi:

And enter the command to remove the BOM:

Save the file:

眼眸里的那抹悲凉 2024-09-17 08:36:40

BOM 只是一个字符序列(UTF-8 为 $EF $BB $BF),因此只需使用脚本删除它们或配置编辑器,这样就不会添加它们。

来自 从 UTF-8 中删除 BOM

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

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

BOM is just a sequence of characters ($EF $BB $BF for UTF-8), so just remove them using scripts or configure the editor so it's not added.

From Removing BOM from UTF-8:

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

I am sure it translates to PHP easily.

迷爱 2024-09-17 08:36:40

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

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

I don't know PHP, so I don't know if this is possible, but the best solution would be to read the file as UTF-8 rather than some other encoding. The BOM is actually a ZERO WIDTH NO BREAK SPACE. This is whitespace, so if the file were being read in the correct encoding (UTF-8), then the BOM would be interpreted as whitespace and it would be ignored in the resulting CSS file.

Also, another advantage of reading the file in the correct encoding is that you don't have to worry about characters being misinterpreted. Your editor is telling you that the code page you want to save it in won't do all the characters that you need. If PHP is then reading the file in the incorrect encoding, then it is very likely that other characters besides the BOM are being silently misinterpreted. Use UTF-8 everywhere, and these problems disappear.

墨离汐 2024-09-17 08:36:40

对我来说,这很有效:

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

如果我删除这个元,“ï”就会再次出现。希望这对某人有帮助...

For me, this worked:

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

If I remove this meta, the  appears again. Hope this helps someone...

跨年 2024-09-17 08:36:40

可以用

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

awk 替换似乎可以,但是不太到位。

You can use

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

Replacing with awk seems to work, but it is not in place.

寂寞花火° 2024-09-17 08:36:40

grep -rl $'\xEF\xBB\xBF' * | xargs vim -e -c 'argdo 设置文件编码=utf-8|设置编码=utf-8|设置nobomb| wq'

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

动听の歌 2024-09-17 08:36:40

我的一些 PHP 文件中出现的 BOM 也遇到了同样的问题 (?????)。

如果您使用 PhpStorm 您可以在设置 -> 中设置热键将其删除。 IDE设置->键盘映射 ->主菜单->文件->删除物料清单。

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.

冷清清 2024-09-17 08:36:40

在 Notepad++ 中,选择“编码”菜单,然后选择“以不带 BOM 的 UTF-8 进行编码”。然后保存。

请参阅堆栈溢出问题如何让记事本以UTF-8无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?.

谜兔 2024-09-17 08:36:40

在 Notepad++ 中打开有问题的 PHP 文件。

单击顶部的“编码”,然后从“无 BOM 的 UTF-8 编码”更改为“UTF-8 编码”。保存并覆盖服务器上的文件。

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.

孤檠 2024-09-17 08:36:40

同样的问题,不同的解决方案。

PHP 文件中的一行打印出 XML 标头(使用与 PHP 相同的开始/结束标记)。看起来这些标签中的代码设置了编码,并在 PHP 中执行,导致了奇怪的字符。不管怎样,解决方案如下:

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

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

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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

# fixed
$xml_string = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\"?" . ">";
逆光飞翔i 2024-09-17 08:36:40

如果您需要能够从 UTF-8 编码文件中删除 BOM,您首先需要找到一个能够识别它们的编辑器。

我个人使用E文本编辑器

右下角有字符编码选项,包括BOM标签。加载文件,取消选择“字节顺序标记”(如果已选择),重新保存,然后就应该完成了。

替代文本 http://oth4.com/encoding.png

E 不是免费的,但有一个免费试用,它是一个出色的编辑器(TextMate 兼容性有限)。

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

独自←快乐 2024-09-17 08:36:40

您可以通过 PhpStorm 打开它,然后右键单击您的文件,然后单击删除 BOM ...

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

掀纱窥君容 2024-09-17 08:36:40

这是解决 BOM 问题的另一个好方法。这是两个 VBScript (.vbs) 脚本。

一种是在文件中查找 BOM,另一种是删除文件中该死的 BOM。它工作得很好而且很容易使用。

只需创建一个 .vbs 文件,并将以下代码粘贴到其中。

您只需将可疑文件拖放到 .vbs 文件上即可使用 VBScript 脚本。它会告诉您是否有 BOM。

' 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

如果它告诉您有 BOM,请使用以下代码创建第二个 .vbs 文件,并将可疑文件拖到该 .vbs 文件上。

' 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

代码来自 Heiko Jendreck< /a>.

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.

坦然微笑 2024-09-17 08:36:40

在PHPStorm中,对于多个文件且BOM不一定位于文件开头,可以搜索\x{FEFF}(正则表达式)并替换为空。

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.

吃兔兔 2024-09-17 08:36:40

同样的问题,但它只影响一个文件,所以我只是创建了一个空白文件,将代码从原始文件复制/粘贴到新文件,然后替换原始文件。不花哨但它有效。

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.

表情可笑 2024-09-17 08:36:40

使用 Total Commander 搜索所有 BOMed 文件:

搜索带有 BOM 的 UTF-8 文件的优雅方法?

  • 在适当的编辑器(可识别 BOM)中打开这些文件,例如 Eclipse

  • 将文件的编码更改为 ISO(右键单击,属性)。

  • 从文件开头剪切 ,保存

  • 将文件的编码改回 UTF-8

...甚至不要再考虑使用 n...d!

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!

盛装女皇 2024-09-17 08:36:40

我也有同样的问题。问题是因为我的一个 php 文件是 utf-8 格式的(最重要的是,所有 php 文件中都包含配置文件)。

就我而言,我有两种适合我的不同解决方案:

首先,我通过在配置文件(或 .htaccess)中使用 AddDefaultCharsetDirective 更改了 Apache 配置。此解决方案强制 Apache 使用正确的编码。

AddDefaultCharset ISO-8859-1

第二个解决方案是更改 php 文件的错误编码。

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.

遮了一弯 2024-09-17 08:36:40
  1. 复制 filename.css 文件的文本。
  2. 关闭你的 css 文件。
  3. 将其重命名为 filename2.css 以避免文件名冲突。
  4. 在 MS 记事本或写字板中,创建一个新文件。
  5. 将文本粘贴到其中。
  6. 将其保存为 filename.css,从编码选项中选择 UTF-8。
  7. 上传文件名.css。
  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.
深空失忆 2024-09-17 08:36:40

这对我有用!

def removeBOMs(fileName):
     BOMs = ['',#Bytes as CP1252 characters
    'þÿ',
    'ÿþ',
    '^@^@þÿ',
    'ÿþ^@^@',
    '+/v',
    '÷dL',
    'Ýsfs',
    'Ýsfs',
    '^Nþÿ',
    'ûî(',
    '„1•3']
     inputFile = open(fileName, 'r')
     contents = inputFile.read()
     for BOM in BOMs:
         if not BOM in contents:#no BOM in the file...
             pass
         else:
             newContents = contents.replace(BOM,'', 1)
             newFile = open(fileName, 'w')
             newFile.write(newContents)
             return None

This works for me!

def removeBOMs(fileName):
     BOMs = ['',#Bytes as CP1252 characters
    'þÿ',
    'ÿþ',
    '^@^@þÿ',
    'ÿþ^@^@',
    '+/v',
    '÷dL',
    'Ýsfs',
    'Ýsfs',
    '^Nþÿ',
    'ûî(',
    '„1•3']
     inputFile = open(fileName, 'r')
     contents = inputFile.read()
     for BOM in BOMs:
         if not BOM in contents:#no BOM in the file...
             pass
         else:
             newContents = contents.replace(BOM,'', 1)
             newFile = open(fileName, 'w')
             newFile.write(newContents)
             return None
你另情深 2024-09-17 08:36:40

检查您的 index.php,找到“... charset=iso-8859-1”并将其替换为“... charset=utf-8””。

也许会起作用。

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

Maybe it'll work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文