How to avoid echoing character 65279 in php?

Viewed 45431

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?

12 Answers

I'm using the PhpStorm IDE to develop php pages.

I had this problem and use this option of IDE to remove any BOM characters and problem solved:

File -> Remove BOM

Try to find options like this in your IDE.

A Linux solution to find and remove this character from a file is to use sed -i 's/\xEF\xBB\xBF//g' your-filename-here

My solution is create a php file with content:

<?php
header("Content-Type:text/html;charset=utf-8");
?>

Save it as ANSI, then other php file will require/include this before any html or php code

Related