0

I'm using openinviter class to import the contacts from email. However, it displays the Unicodes of non-english characters (for example, polish), such as u0117 (and similar type of other codes) instead of normal characters. Is there anyway that I can convert the unicodes to characters and then display them?

Here is the original code of the page: http://pastebin.com/d0tkpxbv

Thanks.

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
Lucka
  • 343
  • 1
  • 9
  • 16

3 Answers3

0

The following code allows you to decode the characters as well as re-encode them if necessary

Code :

if (!function_exists('codepoint_encode')) {

    function codepoint_encode($str) {
        return substr(json_encode($str), 1, -1);
    }

}

if (!function_exists('codepoint_decode')) {

    function codepoint_decode($str) {
        return json_decode(sprintf('"%s"', $str));
    }

}

How to use :

header('Content-Type: text/html; charset=utf-8'); 

var_dump(codepoint_encode('ඔන්ලි'));
var_dump(codepoint_encode('සින්ග්ලිෂ්'));

var_dump(codepoint_decode('\u0d94\u0db1\u0dca\u0dbd\u0dd2'));
var_dump(codepoint_decode('\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca'));

Output :

string(30) "\u0d94\u0db1\u0dca\u0dbd\u0dd2"
string(60) "\u0dc3\u0dd2\u0db1\u0dca\u0d9c\u0dca\u0dbd\u0dd2\u0dc2\u0dca"
string(15) "ඔන්ලි"
string(30) "සින්ග්ලිෂ්"

If you want more complex functionality, see How to get the character from unicode code point in PHP?.

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • In your decode function you need to escape double quotes that could be in _$str_. Otherwise bad things will happen. – datashaman Nov 14 '17 at 06:44
0

try this one: http://php.net/manual/en/function.chr.php

fingerman
  • 2,440
  • 4
  • 19
  • 24
0

Use preg_replace() to convert the value to its hexadecimal numeric entity:

<?php
$text = 'Wladisu0142aw';
$text = preg_replace('/u([0-9a-f]{4})/i', '&#x$1;', $text);
echo $text;  //displays Wladisław
?>
David Powers
  • 1,644
  • 12
  • 11
  • This does not address the _backslash_ that is usually just before the _u_. Those will be left behind in the result. The _$text_ variable should be: _Wladis\u0142aw_ – datashaman Nov 14 '17 at 06:47