debug strange characters returned by symfony - php

I'm not sure how to debug this, or even how best to describe the problem, but all symfony requests are returning strange characters at the beginning of every page. Example:
§{"id":"c8184631","version":0.1}
This should be a json response. Those two characters appear at the beginning of every response no matter the bundle or controller. But only happens on symfony, regular PHP on that server is fine.
This doesn't happen locally. I'm unsure how to start debugging this or even which questions to ask.

Maybe there are some files with different encoding (utf-8 or iso-8859-13), that happened to me before, but I was not using symfony2, just php.
What I did was open every file and changed the file encoding to utf-8.
You can check the encoding for each file for example in "Notepad ++"
Encoding->Convert to UTF-8.
It worked for me.

Related

PHP doesn't show some characters properly

I am running a PHP CLI script on my local machine, which calls an API and receives back UTF encoded string. Now, this string is basically a simple word in Portuguese, and I can see it properly when printed on the screen(terminal). However, on a different machine, some characters are not shown properly. The question is, which php.ini option would I need to set in order to see string properly on the other machine? I haven't touched my PHP.ini in regards to encoding and everything works fine, so I'm not sure what do I need to enable for UTF to work on a php cli app?
Edit: this should not be a terminal issue. To be more specific, API returns back a string(UTF encoded) which is supposed to be found inside an array. However, PHP is issuing a warning on another machine saying that can't find that string inside an array. It is the key doesn't exist error that I'm getting, and the key or the string that is shown doesn't look right, so that's the case.
It is basically
$stringReturnedFromApi = $apiCall();
$this->myArray[$stringReturnedFromApi];
it works right on my machine, on another is complaining that the key doesn't exist, and when I looked up $stringReturnedFromAp, it doesn't look UTF encoded
The other terminals need to be set to UTF-8 encoding. For instance, PuTTY needs to be told what encoding, or he will use (on windows) ANSI if i remember well.
If the other host is not on Windows, just verify the locales to be sure it is UTF-8. This is not on your side that the error is, this is just an encoding which is client-side.
If the result is printed out to a webpage, be sure the charset encoding is set to UTF8 also;
I am not sure that this is actually a PHP problem.
Without seeing your script I can't say anything 100%, but I am guessing that the PHP script is actually correctly outputting the UTF character. The reason it looks odd is because the terminal doesn't understand UTF encoding and is unable to display the character.
You would need to be a little more specific on the details of the terminal application you are using to determine where the actual problem lies. I doubt it is something you can fix in the php.ini

Find source of BOM in Zend Framework 2

I realized that all response that returns my Zend Framework 2 application contains weird characters at the beginning. For example, when I copy the source code of any page returned by ZF2, I see these characters at the beginning of the file when I paste it in Notepad++ : . These seem to be 6 Byte Order Mark characters following each other.
I checked the encoding of my files, and every file I opened in Notepad++ were said to be in UTF-8 w/o BOM.
Also, I checked other pages on my server from other sites, and there is no problem.
Could you please help me understand why there is such a thing at the beginning of each page of my site, even in the Json data returned by my web services. What would be the quickest way to spot from where these are printed and how to get rid of them?
Thank you for your help.
I eventually found my answer here:
Elegant way to search for UTF-8 files with BOM?
I tried both ways described on the thread:
grep -rl $'\xEF\xBB\xBF' .
or using Total Commander available here.
It helped me find the files where the BOM character appears and was then able to convert these files to UTF-8 w/o BOM.

Newline at top of PHP output

I have a strange problem about an additional line above my PHP output.
I'm using the Smarty template engine for formatting my output on a webpage. After I programmed a API connection, I get a newline above the output of the added subpage. Of course, I googled this problem. What I've tried so far:
deleting all newlines at the end of all files and saving them again as UTF-8 without BOM
auto_prepend_file in php.ini is set to nothing
deleting the Smarty cache, turn caching off
One other strange thing is that if I test the program on my local PC (using XAMPP), I get the newline on every page. But when I test the system on my customers live server, only the affected API-page gets this newline. PHP warnings (E_ALL) appear above the newline.
Anyone got a idea that I haven't tried yet?
Anyone got a idea that I haven't tried yet?
Well, as we cannot see the code, and while this is not an ideal or robust testing scenario, in your case this might be useful.
In the files where you think the error may be, starting with the first file which runs, add something like exit('Here');.
Then run the code and see if the new line is there before the exit message, if not the issue is after that exit().
Delete the exit() from that file (make sure you do this!), then add it to the next file in the run order.
Run the code again, if no issue, repeat through the files in load order until the issue presents itself.
When the issue is present, you know the issue is between the code/file from the last time you exited and the current exit.
Examine that code carefully, the issue will be there.
Again, this is a little tacky and not a great test or debug method, but it can be helpful and sometimes necessary without a testing environment in place.
Also adding, as #Fred-ii- suggested, removing the ?> tags at the end of PHP files can help, if you have them (in fact you shouldn't have them there, this used to be a requirement many moons ago but generally just causes issues/headaches).

Intermittent problem with UTF-8 characters

I am running a fairly standard LAMP stack.
The problem is an intermittent rendering of UTF-8 characters correctly. About 50% of the time the non-ASCII UTF-8 characters render correctly (e.g. with appropriate diacritical marks), but about 50% of the time I get the '?' rendition instead. If I reload the page, sometimes it corrects the problem and sometimes it does not. It happens with all browsers on all platforms, which suggests a MYSQL or Apache problem but I have not been able to figure it out.
The data base itself is in UTF-8 format and I have never seen the problem while browsing the database in phpMyAdmin.
I issue a SET NAMES utf-8 command upon opening the data base (and have tried changing that to a SET CHARSET utf-8 command) with no luck.
What's confusing me is that it is intermittent, happening in streaks, e.g. it will happen on 30 pages in a row (even if they are just reloads), and then clear up for 10 pages, and then happen again for a few pages, etc.
You can try to see the problem by hitting the 'list' button here: http://latin-words.com/list_vocab.php though it may take many reloads to either make it happen or make it go away
Server Configuration:
Ubuntu: 9.10
Mysql: 5.1.37
PHP 5.2.10
Apache 2.2.12
Any hints would be greatly appreciated?
edit:
For searchers sake, from the comments, the problem was actually an issue doing a SET NAMES utf-8; (incorrect) instead of an SET NAMES utf8; (correct) That doesn't mean my much more obscure reason posted below cannot also be the reason ;)
Sounds like a problem with locales & iconv, try to determine what locale is used in the webserver process the moment all is well, and the moment it doesn't work anymore (try $currentlocale = setlocale(LC_ALL,NULL); or $currentlocale = setlocale(LC_CTYPE ,NULL); to get the used locale).

PHP: xml_parse replaces entities and "?" character

How can I describe something I can't quite put my finger on?
I have a module for RSS reading "magpierss" it uses xml_parse() and after extensive troubleshooting all I can come up with is that it removes certain entities like "?" character.
I haven't written this module myself but I have traced the code until the part uses xml_parse() and then the output is just replaced.
And to my disappointment this behavior exists on our production server only and not the local server.
I have compared both php.ini file to search for any differences but couldn't find anything.
Could anyone please direct on where to search exactly, i don't even know which direction to start from.
Thanks
mb_string was enabled on the server
and somehow it produces this unexpected behavior with xml_parse()

Categories