为什么是 出现在我的 HTML 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9691771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is  appearing in my HTML?
提问by mehdi
I see this character in Firebug 
.
我在 Firebug 中看到了这个角色
。
I don't know why this is happening, there's no such character in my code. For Firefox it's OK, but in IE everything breaks. I can't even search for this character in Google.
我不知道为什么会这样,我的代码中没有这样的字符。对于 Firefox 来说没问题,但在 IE 中一切都中断了。我什至无法在 Google 中搜索此字符。
I saved my file with utf-8 encoding without bom.
我用没有 bom 的 utf-8 编码保存了我的文件。
回答by RHSeeger
The character in question 
is the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). It may be that you copied it into your code via a copy/paste without realizing it. The fact that it's not visible makes it hard to tell if you're using an editor that displays actual unicode characters.
有问题的字符
是 Unicode 字符“零宽度无间断空间”(U+FEFF)。可能是您在没有意识到的情况下通过复制/粘贴将其复制到您的代码中。它不可见的事实使得很难判断您是否正在使用显示实际 unicode 字符的编辑器。
One option is to open the file in a very basic text editor that doesn't understand unicode, or one that understands it but has the ability to display any non-ascii characters using their actual codes.
一种选择是在不理解 unicode 的非常基本的文本编辑器中打开文件,或者在理解它但能够使用其实际代码显示任何非 ascii 字符的文本编辑器中打开文件。
Once you locate it, you can delete the small block of text around it and retype that text manually.
找到它后,您可以删除它周围的小块文本并手动重新键入该文本。
回答by shonhloi
Just use notepad ++ with encoding UTF-8 without BOM.
只需使用记事本 ++ 编码 UTF-8,无需 BOM。
回答by user1386086
yeah, its so simple to fix that, just open that file by notepad++ and step follow --> Encoding\ encoding UTF-8 without BOM. then save that. It work for me as well!
是的,解决这个问题很简单,只需通过记事本++打开该文件,然后执行步骤 --> Encoding\ encoding UTF-8 without BOM。然后保存。它也适用于我!
回答by Elhmahmy
Try:
尝试:
<?php
// Tell me the root folder path.
// You can also try this one
// $HOME = $_SERVER["DOCUMENT_ROOT"];
// Or this
// dirname(__FILE__)
$HOME = dirname(__FILE__);
// Is this a Windows host ? If it is, change this line to $WIN = 1;
$WIN = 0;
// That's all I need
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF8 BOM FINDER and REMOVER</title>
<style>
body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; }
.FOUND { color: #F30; font-size: 14px; font-weight: bold; }
</style>
</head>
<body>
<?php
$BOMBED = array();
RecursiveFolder($HOME);
echo '<h2>These files had UTF8 BOM, but i cleaned them:</h2><p class="FOUND">';
foreach ($BOMBED as $utf) { echo $utf ."<br />\n"; }
echo '</p>';
// Recursive finder
function RecursiveFolder($sHOME) {
global $BOMBED, $WIN;
$win32 = ($WIN == 1) ? "\" : "/";
$folder = dir($sHOME);
$foundfolders = array();
while ($file = $folder->read()) {
if($file != "." and $file != "..") {
if(filetype($sHOME . $win32 . $file) == "dir"){
$foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
} else {
$content = file_get_contents($sHOME . $win32 . $file);
$BOM = SearchBOM($content);
if ($BOM) {
$BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
// Remove first three chars from the file
$content = substr($content,3);
// Write to file
file_put_contents($sHOME . $win32 . $file, $content);
}
}
}
}
$folder->close();
if(count($foundfolders) > 0) {
foreach ($foundfolders as $folder) {
RecursiveFolder($folder, $win32);
}
}
}
// Searching for BOM in files
function SearchBOM($string) {
if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
return false;
}
?>
</body>
</html>
copy this code to php file upload to root and run it.
将此代码复制到php文件上传到root并运行它。
for more about this: http://forum.virtuemart.net/index.php?topic=98700.0
回答by ferg
"I don't know why this is happening"
“我不知道为什么会这样”
Well I have just run into a possible cause:-) Your HTML page is being assembled from separate files. Perhaps you have files which only contain the body or banner portion of your final page. Those files contain a BOM (0xFEFF) marker. Then as part of the merge process you are running HTML tidy or xmllint over the final merged HTML file.
好吧,我刚刚遇到了一个可能的原因:-) 您的 HTML 页面是由不同的文件组合而成的。也许您的文件只包含最终页面的正文或横幅部分。这些文件包含一个 BOM (0xFEFF) 标记。然后作为合并过程的一部分,您将在最终合并的 HTML 文件上运行 HTML tidy 或 xmllint。
That will cause it!
那会造成的!
回答by JohnA10
If you are using Notepad++, "Menu" >> "Encoding" >> "Convert to UTF-8" your "include" files.
如果您使用 Notepad++,“菜单”>>“编码”>>“转换为 UTF-8”您的“包含”文件。
回答by Henry Gabriel González Montejo
If you have a lot of files to review, you can use this tool: https://www.mannaz.at/codebase/utf-byte-order-mark-bom-remover/
如果你有很多文件要查看,可以使用这个工具:https: //www.mannaz.at/codebase/utf-byte-order-mark-bom-remover/
Credits to Maurice
归功于莫里斯
It help me to clean a system, with MVC in CakePhp, as i work in Linux, Windows, with different tools.. in some files my design was break.. so after checkin in Chrome with debug tool find the  error
它帮助我使用 CakePhp 中的 MVC 清理系统,因为我在 Linux、Windows 中使用不同的工具工作......
回答by O?uzhan Sari
Before clear space (trim)
在清除空间之前(修剪)
Then replace with RegEx .replace("/\xEF\xBB\xBF/", "")
然后用RegEx替换 .replace("/\xEF\xBB\xBF/", "")
I'm working on Javascript, I did with JavaScript.
我正在研究 Javascript,我使用 JavaScript。
回答by Marcello
An old stupid trick that works in this case... paste code from your editor to ms notepad, then viceversa, and evil character will disappears ! I take inspiration from wyisyg/msword copypaste problem. Notepad++ utf-8 w/out BOM works as well.
在这种情况下有效的一个古老的愚蠢技巧......将代码从您的编辑器粘贴到 ms 记事本,然后反之亦然,邪恶的角色将消失!我从 wyisyg/msword 复制粘贴问题中获得灵感。Notepad++ utf-8 w/out BOM 也能工作。
回答by kurdtpage
Here's my 2 cents: I had the same problem and I tried using Notepad++ to convert to UTF-8 no BOM, and also the old "copy to MS notepad then back again" trick, all to no avail. My problem was solved by making sure all files (and 'included' files) were the same file system; I had some files that were Windows format and some that had been copied off a remote Linux server, so were in UNIX format.
这是我的 2 美分:我遇到了同样的问题,我尝试使用 Notepad++ 转换为无 BOM 的 UTF-8,以及旧的“复制到 MS 记事本然后再返回”技巧,但都无济于事。我的问题是通过确保所有文件(和“包含”文件)都是相同的文件系统来解决的;我有一些是 Windows 格式的文件,还有一些是从远程 Linux 服务器复制的,所以是 UNIX 格式。