May 4, 2012

converting unicode u sequence (like u041a) to normal utf-8 text in php

Question by QuadroVal

I have a string like this
“u041au043bu0443u0431 Test”;

It was decoded by json_encode(), the original string was “Клуб Test” in russian.
when I put it to js like

alert(“u041au043bu0443u0431 Test”);

I get correct displaying, like on the screen. So js
in some way poperly decodes it to normal view.
The question is how can I do the same thing in php, is there any built in method?

enter image description here


THE ANSWER IS:
$json_in = '{"testKey":"u041au043bu0443u0431 Test"}';
$json_out = json_decode($json_in, true);
or
Convert  "u041au043bu0443u0431" to  "Клуб" 
and perform html_entity_decode($str, null, 'UTF-8');

Answer by globin

You probably want HTML entities to print the characters:

  • Ӓ for decimal code
  • Ī for hex code

Answer by Starx

While converting the data, use JSON_UNESCAPED_UNICODE as the options

echo json_encode($text, JSON_UNESCAPED_UNICODE);
April 29, 2012

PHP UTF-8 Encoding in HTML Input Box

Question by Wasim

Here what is in database

Seilpendel für Tragsysteme

now i am loading it into textbox via AJAX but what loads into textbox is

Seilpendel f�r Tragsysteme

but i want correct string Seilpendel für Tragsysteme into inputbox

i have tried

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

but not its working

Answer by mcdado

Make sure the db is encoded in UTF8, or use utf8_encode() or utf8_decode() when loading data from db or when writing back.

Answer by Starx

The correct way solving this problem is to configure database to use UTF8 as their character encoding in and out.

RUN The following query after connecting to the database, one time and everything should start working.

SET NAMES 'utf8';
April 6, 2012

UTF-8 charset issues from MySQL in PHP

Question by Nick

this is really doing my nut…..

all relevant PHP Output scripts set headers (in this case only one file – the main php script):

header("Content-type: text/html; charset=utf-8");

HTML meta is set in head:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

all Mysql tables and related columns set to:

utf8_unicode_ci     Unicode (multilingual), case-insensitive

I have been writing a class to do some translation.. when the class writes to a file using fopen, fputs etc everything works great, the correct chars appear in my output files (Which are written as php arrays and saved to the filesystem as .php or .htm files. eval() brings back .htm files correctly, as does just including the .php files when I want to use them. All good.

Prob is when I am trying to create translation entries to my DB. My DB connection class has the following line added directly after the initial connection:

 mysql_query("SET NAMES utf8, character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

instead of seeing the correct chars, i get the usual crud you would expect using the wrong charset in the DB. Eg:

Propriétés

instead of:

propriétés

don’t even get me started on Russian, Japanese, etc chars! But then using UTF8 should not make any single language charset an issue…

What have I missed? I know its not the PHP as the site shows the correct chars from the included translation .php or .htm files, its only when I am dealing with the MySQL DB that I am having these issues. PHPMyAdmin shows the entries with the wrong chars, so I assume its happening when the PHP “writes” to MySQL. Have checked similar questions here on stack, but none of the answers (all of which were taken care of) give me any clues…

Also, anyone have thoughts on speed difference using include $filename vs eval(file_get_contents($filename)).

Answer by Sebastián Grignoli

You say that you are seeing “the usual crud you would expect using the wrong charset”. But that crud is in fact created by using utf8_encode() on an already UTF8 string, so chances are that you are not using the “wrong encoding” anywhere, but exceeding the times you are encoding into UTF8.

You may take a look into a library I made to fix that kind of problems:

http://stackoverflow.com/a/3521340/290221

Answer by Starx

There is a mysql_set_charset('utf8'); in mysql for that. Run the query at the beginning of another query.

...

Please fill the form - I will response as fast as I can!