如何避免在PHP呼应的性格65279? (这个问题也涉及到的Javascript xmlhttp.responseText(阿贾克斯)) [英] How to avoid echoing character 65279 in php? (This question also relates to Javascript xmlhttp.responseText (ajax))

查看:129
本文介绍了如何避免在PHP呼应的性格65279? (这个问题也涉及到的Javascript xmlhttp.responseText(阿贾克斯))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也遇到了类似的问题描述这里(和其他地方) - 其中,作为一个Ajax回调,我收到了xmlhttp.responseText这似乎确定(当我提醒它 - 它显示了正确的文字) - 但是当使用一个'如果'语句来比较它的字符串 - 返回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.

(我也是谁写的服务器端code返回该字符串中的一个) - 经过多次研究串 - 我发现该字符串有一个隐形人物作为自己的第一个字符。这是没有显示的字符。如果我把它复制到记事本 - 然后被删除的第一个字符 - 它不会删除到pressing再次删除

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

我做了一个字符$ C $猫(0)在xmlhttp.responseText返回的字符串。而它返回的 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?

推荐答案

要总结,并指定解决方案:

To conclude, and specify the solution:

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文件到另一个 - 然后,事情变得复杂和字符串被显示ppended他们的性格(65279)$ P $。

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.

您可以编辑与其他文本编辑器如记事本++的文件,并使用编码
恩code在UTF-8无BOM,
这似乎解决问题。

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? (这个问题也涉及到的Javascript xmlhttp.responseText(阿贾克斯))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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