How to handle this PHP encoding issue? - php

I'm using PHP to grab some JSON from this URL - goo.gl/xdIqEy
I've tried using cURL and file_get_contents(), but the 'LastName' element always comes back looking like ─Éur─æi─ç
If I view that URL in Chrome, it looks like ÄurÄ‘ić
It should look like Đurđić
It's obviously some kind of encoding issue, but how to I handle this? The HTTP response headers don't give any clues to the encoding type.
I've tried a lot of different iconv combinations when I've got the string back in PHP - but no luck.
If I go to that URL in IE, it let's me download the .json file to disk. When I open that in Sublime Text, it looks correct.
Any advice?

You can use:
<?php
header('Content-Type: text/html; charset=utf-8');
echo iconv("utf-8", "latin1", "BosanÄić");//Bosanči
echo iconv("utf-8", "latin1", "JiráÄek"); //Jiráček
?>
Your input charset is UTF-8 you need to convert back to latin1

Related

PHP SimpleXMLElement special character encoding / decoding

I've got the folowing xml string in php:
$str="<name>An☢maly</name>";
I then do the following:
$tmp=new SimpleXMLElement($str);
if I do:
echo $tmp->name;
I get this:
An☢maly
When I do:
echo utf8_decode($tmp->name);
I get:
An?maly
How do I get PHP to display that funny character, it also does it with the following:
ƎnƔy™ = ?n?y?
Try use
header('Content-type:text/html;charset=utf-8');
before output (if you use browser)
P.S: An☢maly - this is output when browser render page in "ISO-8859-1" charset instead "UTF-8"
That's strange, I'm doing the exact same thing and it is displaying properly in web browser:
<?php
$str="<xml><name>ƎnƔy™</name></xml>";
$tmp=new SimpleXMLElement($str);
echo $tmp->name;
?>
I noticed that it didn't work properly unless I saved the source file as a UTF8 encoded file. I'm using Programmer's Notepad and clicked file > properties and changed the 'Encoding' parameter to UTF-8 to achieve this.
Also, UTF8 characters will not print correctly on windows command line, which may also be your problem.

Sending UTF8 in GET parameter

When navigating to a URL like this:
http://example.com/user?u=ヴィックサ
I notice that Chrome encodes the characters as:
http://example.com/user?u=%E3%83%B4%E3%82%A3%E3%83%83%E3%82%AF%E3%82%B5
And everything works serer-side.
However, in IE I get this error from my code:
The user you are trying to find (?????) does not exist.
Note the five question marks. For some reason the PHP never gets to see the parameter.
What could be causing this, and is there any way to fix it?
Sadly it seems what you want to do is not going to work for the current generation of IE
The accepted answer for this question UTF-8 Encoding issue in IE query parameters says that you need to encode the characters yourself rather than relying on the browser as support varies from browser to browser, and maybe even device to device
<a href='/path/to/page/?u=<?=urlencode('ヴィックサ')?>'>View User</a>
Also I presume you are setting utf8 headers from the webserver? you didn't say, if not, in php
header('Content-Type: text/html; charset=utf-8');

Encoding problem in PHP while making a webservice call

I have the nth problem encoding related with PHP!
so the story is:
i read a url from a file (ISO-8859). I cant change the encoding of this file for various reason I wont discuss here.
I use that url to make a call to a rest webservice.
the url happens to contain the symbol "è" which is conveted to � when it is loaded by the PHP engine.
as a result the webservice returns and unexpected result because what it gets is actually the word "perch�" instead of "perchè".
I tried to force php to work with ISO-8859 by doing:
ini_set('default_charset', "ISO-8859");
The problem is that it still doesn't work and the webservice doesn't answer properly. I am sure that the webservice works as I tried to copy paste the url by hand in a browser and I received the expected data.
You can convert data from one character set into another using iconv().
Your REST web service is most likely expecting UTF-8 data, so you would have to do something like this:
$data = iconv("iso-8859-1", "utf-8", $data);
before sending the request.

PHP - Encoding strikes again

I'm having encoding problems in my webpage, and it is driving me crazy. Let me try to explain
I have a meta tag defining utf8 as charset.
I'm including the scripts as utf8 too (<script type="text/javascript src="..." charset="utf8"></script>).
In .php files, I declare header('Content-Type: text/html; charset=utf8');
In my database (postgreSQL), I've made the query show lc_collate; and the return was en_US.UTF-8
I'm using AJAX
When I try to save the field value "name" as "áéíóú", I get the value "áéíóú" in the record set (using phpPgAdmin to view results).
What am I doing wrong? There's a way to fix it without using decode/encode? Someone have a good reference about theses issues?
Thank you all!
Maybe the client encoding is not set correctly? PostgreSQL automatically converts between the character encoding on the client and the encoding in the database. For this to work it needs to know what encoding the client is using. Safest is to set this when you open your connection using:
SET CLIENT_ENCODING TO 'UTF8';
For details see the docs
You might be storing the data as ISO-8859-1?
Try enconding to base64 and decoding on the other end.

Jquery ajax call and charset windows-1252

Dear stackoveflow, I have this problem. I'm working with an old version of mssql (2000) that has all the tables encoded in windows 1252 (and that's it). I can write and read succesfully with php using this line:
<?php header('Content-Type: text/html; charset=windows-1252'); ?>
If I make a normal post everything works as expected, If I do it ajax style I write messed characters in the table. I've also tried this:
contentType: "application/x-www-form-urlencoded;charset=windows-1252",
With no luck. Can anybody help me?
Thank you
I think it is possible to change the character set for incoming data from the Ajax request in Javascript somehow, butt IIRC, it's complex and is likely to have cross browser issues.
If you are querying a PHP script, the easiest way woudl be to convert the data to UTF-8 there:
$data = "Höllo, thüs üs windows-1252 encoded data";
$data_utf8 = iconv("windows-1252", "utf-8", $data);
echo $data;

Categories