如何避免在PHP中回显字符65279? [英] How to avoid echoing character 65279 in php?

查看:84
本文介绍了如何避免在PHP中回显字符65279?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处(以及其他地方)遇到了类似的问题- 像在ajax回调上一样,我得到一个看起来不错的xmlhttp.responseText(当我警告它时-它显示正确的文本)-但是当使用'if'语句将其与字符串进行比较时-它返回false.

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.

(我也是编写返回该字符串的服务器端代码的人)-在研究了字符串之后-我发现该字符串的第一个字符为不可见字符".未显示的字符.如果我将其复制到记事本中-然后删除了第一个字符-直到再次按Delete键,它才会删除.

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

我为xmlhttp.responseText中返回的字符串做了一个charCodeAt(0).然后返回 65279 .

I did a charCodeAt(0) for the returned string in xmlhttp.responseText. And it returned 65279.

谷歌搜索它表明它是某种UTF-8控制字符,应该设置大端"或小端"编码.

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.

所以,现在我知道了问题的原因-但是...为什么要回显该字符? 在源PHP中,我只是使用

So, now I know the cause of the problem - but... why does that character is being echoed? In the source php I simply use

echo 'the string'...

它显然以某种方式输出[chr(65279)]字符串...

and it apparently somehow outputs [chr(65279)]the string...

为什么?而我该如何避免呢?

Why? And how can I avoid it?

推荐答案

总结并指定解决方案:

Windows记事本将BOM字符(3个字节:EF BB BF)添加到以utf-8编码保存的文件中.

Windows Notepad adds the BOM character (the 3 bytes: EF BB BF) to files saved with utf-8 encoding.

PHP似乎并没有受到它的困扰-除非您将一个php文件包含到另一个文件中- 事情就会变得凌乱,字符串会显示在前面,并带有字符(65279).

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.

您可以使用其他文本编辑器(例如Notepad ++)编辑文件并使用编码
在没有BOM的情况下以UTF-8编码",
这似乎可以解决问题.

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.

此外,您可以在记事本中保存其他具有ANSI编码的php文件-这似乎也可以工作(也就是说,如果您实际上在文件中不使用任何扩展字符,我想...)

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

这篇关于如何避免在PHP中回显字符65279?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆