How to avoid echoing character 65279 in php? (This

2019-01-02 20:55发布

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.

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

I did a charCodeAt(0) for the returned string in xmlhttp.responseText. And it returned 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.

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

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

Why? And how can I avoid it?

9条回答
不流泪的眼
2楼-- · 2019-01-02 21:29

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.

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 𐃁 character at the spot where the PHP code would start.

I also tried to assign the result of the include to a variable ... i.e $result = include("myfile.txt"); with the same extra character being added

Please note that moving the include above the HTML would not remove the extra character from showing, however it removes it from my data and out of the content area.

查看更多
梦醉为红颜
3楼-- · 2019-01-02 21:29

In addition to the above, I just had this issue when pulling some data from a MySQL database (charset is set to UTF-8) - the issue being the HTML tags, I allowed some basic ones like <p> and <a> when I displayed it on the page, I got the &#65729 character looking through Dev Tools in Chrome.

So I removed the tags from the table and that removed the &#65729 issue (and the blank line above the where the text was to be displayed.

I just wanted to add to this, since my Rep isn't high enough to actually comment on the answer.

EDIT: Using VIM I was able to remove the BOM with :set nobomb and you can confirm the presence of the BOM with :set bomb? which will display either bomb or nobomb

查看更多
深知你不懂我心
4楼-- · 2019-01-02 21:41

If you want to print a string that contains the ZERO WIDTH NO-BREAK SPACE char (e.g., by including an external non-PHP file), try the following code:

echo preg_replace("/\xEF\xBB\xBF/", "", $string);
查看更多
墨雨无痕
5楼-- · 2019-01-02 21:43

You can also remove the character in javascript with:

myString = myString.replace(String.fromCharCode(65279), "" );

查看更多
泪湿衣
6楼-- · 2019-01-02 21:48

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.

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.

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.

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

查看更多
荒废的爱情
7楼-- · 2019-01-02 21:48

If you are using Linux or Mac, here is an elegant solution to get rid of the  character in 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:

grep -rl $'\xEF\xBB\xBF' .

This will search for files with BOM. If you have .php results in the list, then do this:

  1. Rename the file to something like filename.bom.bak.php
  2. Open the file in your editor and copy the content in the clipbard.
  3. Create a new file and paste the content from the clipboard.
  4. Save the file with the original name 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.

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.

查看更多
登录 后发表回答