Javascript 如何避免在php中回显字符65279?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6538203/
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
How to avoid echoing character 65279 in php?
提问by Yuval A.
I have encountered a similar problem described here(and in other places) - where as on an ajax callback I get a xmlhttp.responseText that seems ok (when I alert it - it shows the right text) - but when using an 'if' statement to compare it to the string - it returns false.
我遇到了这里(以及其他地方)描述的类似问题- 在 ajax 回调中,我得到一个 xmlhttp.responseText 看起来没问题(当我提醒它时 - 它显示正确的文本) - 但是当使用“if”时语句将其与字符串进行比较 - 它返回 false。
(I am also the one who wrote the server-side code returning that string) - after much studying the string - I've discovered that the string had an "invisible character" as its first character. A character that was not shown. If I copied it to Notepad - then deleted the first character - it won't delete until pressing Delete again.
(我也是编写返回该字符串的服务器端代码的人)-在对该字符串进行了大量研究后-我发现该字符串有一个“不可见字符”作为其第一个字符。未显示的字符。如果我将它复制到记事本 - 然后删除了第一个字符 - 它不会删除,直到再次按下 Delete 键。
I did a charCodeAt(0) for the returned string in xmlhttp.responseText. And it returned 65279.
我在 xmlhttp.responseText 中为返回的字符串做了一个 charCodeAt(0)。它返回65279。
Googling it reveals that it is some sort of a UTF-8 control character that is supposed to set "big-endian" or "small-endian" encoding.
谷歌搜索显示它是某种 UTF-8 控制字符,应该设置“大端”或“小端”编码。
So, now I know the cause of the problem - but... why does that character is being echoed? In the source php I simply use
所以,现在我知道问题的原因了——但是……为什么那个角色会被回响?在源 php 中,我只是使用
echo 'the string'...
and it apparently somehow outputs [chr(65279)]the string...
它显然以某种方式输出 [chr(65279)] 字符串...
Why? And how can I avoid it?
为什么?我怎样才能避免它?
回答by Yuval A.
To conclude, and specify the solution:
总结一下,并指定解决方案:
Windows Notepad adds the BOM character (the 3 bytes: EF BB BF) to files saved with utf-8 encoding.
Windows 记事本将 BOM 字符(3 个字节:EF BB BF)添加到使用 utf-8 编码保存的文件中。
PHP doesn't seem to be bothered by it - unless you include one php file into another - then things get messy and strings gets displayed with character(65279) prepended to them.
PHP 似乎并没有被它打扰 - 除非您将一个 php 文件包含到另一个文件中 - 然后事情会变得混乱并且字符串会显示为字符(65279)。
You can edit the file with another text editor such as Notepad++ and use the encoding
"Encode in UTF-8 without BOM",
and this seems to fix the problem.
您可以使用其他文本编辑器(例如 Notepad++)编辑该文件,并使用
“Encode in UTF-8 without BOM”编码,
这似乎可以解决问题。
Also, you can save the other php file with ANSI encoding in notepad - and this also seem to work (that is, in case you actually don't use any extended characters in the file, I guess...)
此外,您可以在记事本中使用 ANSI 编码保存另一个 php 文件 - 这似乎也有效(也就是说,如果您实际上没有在文件中使用任何扩展字符,我猜...)
回答by matfax
If you want to print a string that contains the ZERO WIDTH NO-BREAK SPACEchar (e.g., by including an external non-PHP file), try the following code:
如果您想打印包含零宽度无间断空间字符的字符串(例如,通过包含外部非 PHP 文件),请尝试以下代码:
echo preg_replace("/\xEF\xBB\xBF/", "", $string);
回答by julianm
If you are using Linux or Mac, here is an elegant solution to get rid of the character in PHP.
如果您使用的是 Linux 或 Mac,这里有一个优雅的解决方案来摆脱 PHP 中的 字符。
If you are using WordPress (25% of Internet websites are powered by WordPress), the chances are that a plugin or the active theme are introducing the BOM character due a file that contains BOM (maybe that file was edited in Windows). If that's the case, go to your wp-content/themes/ folder and run the following command:
如果您使用的是 WordPress(25% 的 Internet 网站由 WordPress 提供支持),则插件或活动主题可能会由于包含 BOM 的文件(可能该文件是在 Windows 中编辑的)而引入 BOM 字符。如果是这种情况,请转到您的 wp-content/themes/ 文件夹并运行以下命令:
grep -rl $'\xEF\xBB\xBF' .
This will search for files with BOM. If you have .php results in the list, then do this:
这将搜索带有 BOM 的文件。如果列表中有 .php 结果,请执行以下操作:
- Rename the file to something like filename.bom.bak.php
- Open the file in your editor and copy the content in the clipbard.
- Create a new file and paste the content from the clipboard.
- Save the file with the original name filename.php
- 将文件重命名为类似 filename.bom.bak.php 的名称
- 在编辑器中打开文件并复制剪贴板中的内容。
- 创建一个新文件并粘贴剪贴板中的内容。
- 使用原始名称 filename.php 保存文件
If you are dealing with this locally, then eventually you'd need to re-upload the new files to the server.
如果您在本地处理此问题,那么最终您需要将新文件重新上传到服务器。
If you don't have results after running the grep command and you are using WordPress, then another place to check for BOM files is the /wp-content/plugins folder. Go there and run the command again. Alternatively, you can start deactivating all the plugins and then check if the problem is solved while you active the plugins again.
如果您在运行 grep 命令后没有结果并且您使用的是 WordPress,那么另一个检查 BOM 文件的地方是 /wp-content/plugins 文件夹。去那里并再次运行命令。或者,您可以开始停用所有插件,然后在再次激活插件时检查问题是否已解决。
If you are not using WordPress, then go to the root of your project folder and run the command to find files with BOM. If any file is found, then run the four steps procedure described above.
如果您没有使用 WordPress,请转到项目文件夹的根目录并运行该命令以查找带有 BOM 的文件。如果找到任何文件,则运行上述四个步骤过程。
回答by Tiago A.
You can also remove the character in javascript with:
您还可以使用以下命令删除 javascript 中的字符:
myString = myString.replace(String.fromCharCode(65279), "" );
myString = myString.replace(String.fromCharCode(65279), "" );
回答by user1156978
I had this problem and changed my encoding to utf-8 without bom, Ansi, etc with no luck. My problem was caused by using a php include function in the html body. Moving the include function to above my html (above !DOCTYPE tag) resolved the issue.
我遇到了这个问题,并在没有 bom、Ansi 等的情况下将我的编码更改为 utf-8。我的问题是由在 html 正文中使用 php 包含函数引起的。将包含函数移动到我的 html 上方(在 !DOCTYPE 标签上方)解决了这个问题。
After I knew my issue I tested include, include_once and require functions. All attempts to include a file from within the html body created the extra miscellaneous