Converting Unicode character to text in PHP is not working - php

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.'"');

Related

How to convert “é” to “é” in PHP?

I'm trying to convert a string from this: “é” to this: “é”. It's a latin1 character but I can't do it right. So far I've tried two functions but none of them give me the right output.
$translation = 'Copà © rnico was Italian';
$translation = mb_convert_encoding($translation, 'utf-8', 'iso-8859-1'); //opt 1
$translation = iconv('utf-8', 'latin1', $translation); //opt 2
I'm getting this data from an Api so I don't know what's going on in the database.
This is the string in Spanish: Copérnico es italiano.
This is the data from the API: Copà © rnico is Italian
This is the result with $translation = bin2hex($translation);
436f70c38320c2a920726e69636f206973204974616c69616e
What's the right way to go? Greetings.
I had the same problem before and this option
$translation = iconv('utf-8', 'latin1', $translation); //opt 2
work verry well.
Your problem is `Copà © rnico was Italian` is not the same than `Copérnico was Italian`.
So when you try to convert the function iconv see 2 wrong UTF-8 symbols because de spaces, is not the same "à © "(2 invalid UTF-8 symbols and 2 spaces) than "é"(1 Valid UTF-8 symbol)

Issue with converting text to Latin

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?

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

Tamil character using JSON php

How to use Tamil character in JSON php
<?php
/* Author : Girija S
Date : 4/21/2011
Description: To Check the Special Chars when we pass in the json server
*/
$text = "தமிழ் அகராதி With the exception <br>of HTML 2.0's ", &, <, and >, these entities are 'all' <br>new<br/> in HTML 4.0 and may not be supported by old browsers. Support in recent browsers is good.The following table gives the character entity <p>reference, decimal character reference, and hexadecimal character reference for markup-significant</p> and internationalization characters\n, as well as the rendering of each in your browser. Glyphs of the characters are available at the Unicode Consortium.<p>This is some text in a paragraph.</p>";
$text = json_encode(utf8_encode($text));
echo $text;
$text = json_decode($text);
echo $text;
?>
"meta http-equiv="Content-Type" content="text/html; charset=UTF-8""
use this in header it will solve the problem..
if you want to store in a data base you should yous
"mysql_query ("set character_set_results='utf8'"); "
before query..
I did like that and got success for my financial tamil application
<?php
//Try it ... working script.. add MIME type and Font characterset in header
header('Content-type="application/json"');
header('charset="utf-8"');
$text = "தமிழ் அகராதி With the exception <br>of HTML 2.0's &quot;, &amp;, &lt;, and &gt;, these entities are &#039;all&#039; <br>new<br/> in HTML 4.0 and may not be supported by old browsers. Support in recent browsers is good.The following table gives the character entity <p>reference, decimal character reference, and hexadecimal character reference for markup-significant</p> and internationalization characters\n, as well as the rendering of each in your browser. Glyphs of the characters are available at the Unicode Consortium.<p>This is some text in a paragraph.</p>";
echo $text = json_encode($text);
echo '<br/><br/><br/>******************************************************************************<br/><br/>';
echo $text = json_decode($text, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
?>

create URL slugs for chinese characters. Using PHP

My users sometimes use chinese characters for the title of their input.
My slugs are in the format of /stories/:id-:name where an example could be /stories/1-i-love-php.
How do I allow chinese characters?
I have googled and found the japanese version of this answer over here.
Don't quite understand Japanese, so I am asking about the chinese version.
Thank you.
i have tested in Bengali characters
it may work. try this:
at first the coded page (write code where in the page) have to convert into encoding type in UTF-8, then write code.
code here:
function to_slug($string, $separator = '-') {
$re = "/(\\s|\\".$separator.")+/mu";
$str = #trim($string);
$subst = $separator;
$result = preg_replace($re, $subst, $str);
return $result;
}
$id=34;
$string_text="আড়াইহাজারে দেড় বছরের --- শিশুর -গলায় ছুরি";
$base_url="http://example.com/";
echo $target_url=$base_url.$id."-". #to_slug($string_text);
var_dump($target_url);
output:
http://example.com/34-আড়াইহাজারে-দেড়-বছরের-শিশুর-গলায়-ছুরি
string 'http://example.com/34-আড়াইহাজারে-দেড়-বছরের-শিশুর-গলায়-ছুরি' (length=136)

Categories