My php script gives out this string (for example) for JSON:
{"time":"0:38:01","kto":"\u00d3\u00e1\u00e8\u00e2\u00f6\u00e0 \u00c3\u00e5\u00ed\u00e5\u00f0\u00e0\u00eb\u00ee\u00e2","mess":"\u00c5\u00e4\u00e8\u00ed\u00fb\u00e9: *mm"}
jQuery code gets this string through JSON:
$.getJSON('chat_ajax.php?q=1',
function(result) {
alert('Time ' + result.time + ' Kto' + result.kto + ' Mess' + result.mess);
});
Browser show:
0:38:01 Óáèâöà Ãåíåðàëîâ
Åäèíûé: *mm
How can I decode this string to cyrillic?
Try use:
<META http-equiv="content-type" content="text/html; charset=windows-1251">
but nothing change
PHP Code:
$res1=mysqli_query($dbc, "SELECT * FROM chat ORDER BY id DESC LIMIT 1");
while ($row1=mysqli_fetch_array($res1)) {
$rawArray=array('time' => #date("G:i:s", ($row1['time'] + $plus)), 'kto' => $row1[kto], 'mess' => $row1[mess]);
$encodedArray = array_map(utf8_encode, $rawArray);
echo json_encode($encodedArray);
PHP ver 5.3.19
\uXXXX stands for unicode characters and in unicode 00d3 is Ó and so on. Unicode characters are unambigouos, so the character encoding of the page is ignored for them. You could use the correct unicode (i.e. \u0443 for У) or write your script so that it outputs the real characters in Windows-1251 instead of unicode sequences.
Update
I see from your comment that you fetch this data from MySQL and use json_encode() to output it. json_encode only works for UTF-8 encoded data (and d3 is Ó in UTF-8 as well, this is why you get the wrong unicode sequences).
So, you will have to convert all data from Windows-1251 to UTF-8 before passing it to json_encode, then everything else will work fine.
Converting:
$utf8Array = array_map(function($in) {
return iconv('Windows-1251', 'UTF-8', $in);
}, $rawArray);
utf8_encode will not work because it is only useful for input in ISO-8859-1 encoding.
I had similar problem when storing json datas in MySQL BDD : this solved the problem :
json_encode($json_data, JSON_UNESCAPED_UNICODE) ;
Related
I am receiving Json response from a webservice so the character Ô turns into \u00d3
How can PETR\u00d3POLIS became PETRÓPOLIS ?
I am using PHP to query the database and return JSON.
After a research from http://www.fileformat.info/info/unicode/char/00d3/index.htm i know the character is Unicode Character 'LATIN CAPITAL LETTER O WITH ACUTE' (U+00D3) .
Wich is the best way to translate these characters ?
Unicode characters are just like escape characters - you can see them in JS string, but they will be displayed properly as a text.
var o = {
text: 'PETR\u00d3POLIS \n\u00a5\u00a5\u00a5'
};
document.body.innerHTML = "<pre>" + o.text + "</pre>";
You can use the below regex
$string = "u00d3";
echo $string = preg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $string)
Since some days I read about Character-Encoding, I want to make all my Pages with UTF-8 for Compability. But I get stuck when I try to convert User-Input to UTF-8, this works on all Browsers, expect Internet-Explorer (like always).
I don't know whats wrong with my code, it seems fine to me.
I set the header with char encoding
I saved the file in UTF-8 (No BOM)
This happens only, if you try to access to the page via $_GET on the internet-Explorer myscript.php?c=äüöß
When I write down specialchars on my site, they would displayed correct.
This is my Code:
// User Input
$_GET['c'] = "äüöß"; // Access URL ?c=äüöß
//--------
header("Content-Type: text/html; charset=utf-8");
mb_internal_encoding('UTF-8');
$_GET = userToUtf8($_GET);
function userToUtf8($string) {
if(is_array($string)) {
$tmp = array();
foreach($string as $key => $value) {
$tmp[$key] = userToUtf8($value);
}
return $tmp;
}
return userDataUtf8($string);
}
function userDataUtf8($string) {
print("1: " . mb_detect_encoding($string) . "<br>"); // Shows: 1: UTF-8
$string = mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string)); // Convert non UTF-8 String to UTF-8
print("2: " . mb_detect_encoding($string) . "<br>"); // Shows: 2: ASCII
$string = preg_replace('/[\xF0-\xF7].../s', '', $string);
print("3: " . mb_detect_encoding($string) . "<br>"); // Shows: 3: ASCII
return $string;
}
echo $_GET['c']; // Shows nothing
echo mb_detect_encoding($_GET['c']); // ASCII
echo "äöü+#"; // Shows "äöü+#"
The most confusing Part is, that it shows me, that's converted from UTF-8 to ASCII... Can someone tell me why it doesn't show me the specialchars correctly, whats wrong here? Or is this a Bug on the Internet-Explorer?
Edit:
If I disable converting it says, it's all UTF-8 but the Characters won't show to me either... They are displayed like "????"....
Note: This happens ONLY in the Internet-Explorer!
Although I prefer using urlencoded strings in address bar but for your case you can try to encode $_GET['c'] to utf8. Eg.
$_GET['c'] = utf8_encode($_GET['c']);
An approach to display the characters using IE 11.0.18 which worked:
Retrieve the Unicode of your character : example for 'ü' = 'U+00FC'
According to this post, convert it to utf8 entity
Decode it using utf8_decode before dumping
The line of code illustrating the example with the 'ü' character is :
var_dump(utf8_decode(html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", 'U+00FC'), ENT_NOQUOTES, 'UTF-8')));
To summarize: For displaying purposes, go from Unicode to UTF8 then decode it before displaying it.
Other resources:
a post to retrieve characters' unicode
This is my code:
<?php
$a = array('chào','thân','ái','và','quyết','thắng');
?>
<script>
var abc = '<?php echo json_encode($a); ?>';
</script>
When run and i got abc variable is:
var abc = '["ch\u00e0o","th\u00e2n","\u00e1i","v\u00e0","quy\u1ebft","th\u1eafng"]';
So, how to convert it's as before? i want to it's become
var abc = '["chào","thân","ái","và","quyết","thắng"]';
There might be you're answer here :
How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?
But why would you translate those escaped sequences, which should be well displayed and correctly translated by php (json_decode) and javascript (JSON.parse) ?
If your php is >= 5.4, you can use the JSON_UNESCAPED_UNICODE flag to encode unicode characters as-is:
var abc = '<?php echo json_encode($a, JSON_UNESCAPED_UNICODE); ?>';
However, the escaped form works just fine and is less error-prone, so I'd suggest that you keep using it.
I want to convert my string to Unicode like if "ग" than give output like "0917" or "917" any one of them.
Link for Unicode of string i want
Please give me a Hint i used ord() but it's not work proper.
$ord = mb_convert_encoding("ग", 'HTML-ENTITIES', 'UTF-8');
echo $ord;
$ord = ord("ग");
echo $ord; // 224 output
Both try but not working.
iconv — Convert string to requested character encoding
http://php.net/manual/en/function.iconv.php
I have a test text which i post with and ajax call (JQuery):
čéáűőúöüó é$ߤ÷׸¨¸˝¨´~˘˝°´˛>*čéáűőúöüó$>*ß$÷×÷;$¨˝´>$đ;ä
i just write the very same text in the response
<?php
$text=$_POST["text"];
echo "\n\nUTF8_DECODE:\n";
echo utf8_decode($text);
echo "\nISO8859-2 -> UTF-8:\n";
echo iconv("ISO-8859-2","UTF-8",$text);
echo "\nUTF-8 -> ISO-8859-2 \n";
echo iconv("UTF-8","ISO-8859-2",$text);
?>
The result should be:
UTF8_DECODE:
?éá??úöüó é$ߤ÷׸¨¸?¨´~??°´?>*?éá??úöüó$>*ß$÷×÷;$¨?´>$?;ä
ISO8859-2 -> UTF-8:
čéáűőúöüó
é$ߤ÷׸¨¸˝¨´~˘˝°´˛>*čéáűőúöüó$>*ß$÷×÷;$¨˝´>$đ;ä
UTF-8 -> ISO-8859:
ĂÂÄĹ ÄÄÄšÄÄšÂÄĹÄĹÄĹşÄĹ ÄĹ
$ÄÂäÄËÄÂøèøĂÂèô~ĂÂĂÂĂ°Ă´ĂÂ>*ĂÂÄĹ
ÄÄÄšÄÄšÂÄĹÄĹÄĹşÄĹ$>*ÄÂ$ÄËÄÂÄË;$èĂÂĂ´>$ĂÂ;Ĥ
But it is:
UTF8_DECODE:
?éá??úöüó é$ߤ÷׸¨¸?¨´~??°´?>*?éá??úöüó$>*ß$÷×÷;$¨?´>$?;ä
ISO8859-2 -> UTF-8:
ĂÂÄĹ ÄÄÄšÄÄšÂÄĹÄĹÄĹşÄĹ ÄĹ
$ÄÂäÄËÄÂøèøĂÂèô~ĂÂĂÂĂ°Ă´ĂÂ>*ĂÂÄĹ
ÄÄÄšÄÄšÂÄĹÄĹÄĹşÄĹ$>*ÄÂ$ÄËÄÂÄË;$èĂÂĂ´>$ĂÂ;Ĥ
UTF-8 -> ISO-8859-2:
čéáűőúöüó
é$ߤ÷׸¨¸˝¨´~˘˝°´˛>*čéáűőúöüó$>*ß$÷×÷;$¨˝´>$đ;ä
My question is why is that?? What i miss?
Because my text is at ISO-8859-2 and i want to transfer to UTF-8, why i need to use the opposite method when:
string iconv ( string $in_charset , string $out_charset , string $str
)
Performs a character set conversion on the string str from in_charset
to out_charset.
Maybe the ajax request encoded in UTF-8 the ISO-8859-2 characters?