Issue with converting text to Latin - php

Need to convert below string from UTF-8 to Latin (ISO-8859-1)
Häger
The correct output should be: Häger
I have tried this by PHP and MySQL but did not get the desired result.
PHP:
<?php
$text = "Häger";
$text= utf8_decode($text);
echo $text;
?>
Output : H�������¤ger
MySQL:
select CONVERT(CAST(CONVERT('Häger' USING latin1) AS binary) USING utf8)
Output : H���¤ger
Can someone please help here?

Related

Converting Unicode character to text in PHP is not working

I am trying to convert Unicode character to text in PHP. But the string is the mixture of Unicode characters and text. But it is not working.
I followed this link (Unicode character in PHP string)
<?php
$unicodeChar = "{'singer': u'', 'name': u'\\\\u101c\\\\u1031\\\\u1011\\\\u1032\\\\u101c\\\\u103d\\\\u103e\\\\u1004\\\\u1037\\\\u103a\\\\u101c\\\\u102d\\\\u102f\\\\u1000\\\\u103a'}\\r\\n\\r\\n artist : Thar Gyi\\r\\n album : Sal Pone Ta Pone\\r\\n genre : R&B\\r\\n copyright : MyanmarSongs.NET\\r\\n track : 1\\r\\n title : Lay Htal Hlwint Lite";
echo json_decode('"'.$unicodeChar.'"');
echo mb_convert_encoding($unicodeChar, 'UTF-8', 'HTML-ENTITIES');
echo mb_convert_encoding($unicodeChar, 'UTF-8', 'UTF-16BE'); showing nothing
?>
All the above scenarios are not working when the value is the mixtures of Unicode characters and text like I used. But it is working when the value is so simple like this:
$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');
How can I achieve this?
use following code
$unicodeChar = '\u1000';
echo json_decode('"'.$unicodeChar.'"');

How to convert unicode in php?

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

utf8 not converting string in PHP

Hello i have german client and i am getting string with german alphabet which i am trying to display properly in output.I tried utf8_encode to convert string but not working for me.
Code:
echo "Desc Short=>". utf8_encode($obj->Desc_Short) . "<br>\r\n";
echo "Desc Long=>". utf8_encode($obj->Desc_Long) . "<br>\r\n";
Output:
Desc Short=>Ablagefach mittig in Gepäckraumtrennwand;ESACO_UG(122)
Desc Long=>Ablagefach mittig in Gepäckraumtrennwand inkl. verschiebbarem Haltenetz
It seems you need to simply use utf8_decode and use php header to set encoding (or set encoding in HTML document).
For the following code:
<?php
header( 'Content-type: text/html; charset=utf-8' );
$x = 'Ablagefach mittig in Gepäckraumtrennwand;ESACO_UG(122)';
echo utf8_decode($x);
Output for this is:
Ablagefach mittig in Gepäckraumtrennwand;ESACO_UG(122)
Your output indicates that the string is already utf-8 encoded.
Either you would have to use utf8_decode() to get the umlaut or - better - change any component in your application to properly handle utf-8. :)
parse the string through utf8_decode function
TRY :
utf8_decode($obj->Desc_Short)
utf8_decode($obj->Desc_Long)

Trouble with decode JSON + PHP

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) ;

Problem in UTF Encoding in PHP

I use the following lines of code:
$revTerm = "". strrev($limitAry["term"]);
$revTerm = utf8_encode($revTerm);
The $revTerm contains Norwegian characters as ø æ å. However, it is shown correctly. I need to reverse them before displaying, so I use the first line.
When I display them this way, I get an error of bad xml format - used to fill a grid.
When I try to use the second line, I don't get an error but the characters are not shown correctly. Could there be any other way to solve that?
If it may help, I use jqGrid to fill those data in.
strrev, like most PHP string functions, is not safe for multi-byte encodings.
try this example
$test = 'А роза упала на лапу Азора ウィキ';
$test = iconv('utf-8', 'utf-16le', $test);
$test = strrev($test);
// キィウ арозА упал ан алапу азор А
echo iconv('utf-16be', 'utf-8', $test);
(russian)
http://bolknote.ru/2012/04/02/~3625#56
Try this:
$revTerm = utf8_decode($limitAry["term"]);
$revTerm = strrev($revTerm);
$revTerm = utf8_encode($revTerm);
For using strrev you have to decode your string to a non-multibyte string.

Categories