Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

php - 如何避免在PHP中回显字符65279?(How to avoid echoing character 65279 in php?)

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.

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

And it returned 65279 .

(然后返回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?

(我该如何避免呢?)

  ask by Yuval A. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(您可以使用其他文本编辑器(例如Notepad ++)编辑文件并使用编码)
"Encode in UTF-8 without BOM",

(“在没有BOM的情况下以UTF-8编码”,)
and this seems to fix the problem.

(这似乎可以解决问题。)

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文件-这似乎也可以工作(也就是说,如果您实际上在文件中不使用任何扩展字符,我想...))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...