edwardsmarkf
edwardsmarkf

Reputation: 1417

convert decimal value to unicode characters in php

i need to convert decimals values into unicode and display the unicode character in PHP.

so for example, 602 will display as this character: ɚ

after referencing this SO question/answer, i was able to piece this together:

echo  json_decode('"' . '\u0' . dechex(602) . '"' );

this seems pretty error-prone. is there a better way to do this?

i was unable to get utf8_encode to work since it seemed to want to start with a string, not a decimal.

EDIT: in order to do characters between 230 and 250, double prefixed zeros are required:

 echo   json_decode('"' . '\u00' . dechex(240) . '"' );  // ð
 echo   json_decode('"' . '\u00' . dechex(248) . '"' );  // ø
 echo   json_decode('"' . '\u00' . dechex(230) . '"' );  // æ

in some cases, no zero is required:

echo json_decode('"' . '\u' . dechex(8592) . '"' );  // ←

this seems strange.

Upvotes: 4

Views: 1394

Answers (4)

nj_
nj_

Reputation: 2339

If you have IntlChar available I'd recommend using IntlChar::chr:

var_dump(IntlChar::chr(602));

Failing that, something like the following avoids any eval/json_decode trickery:

var_dump(iconv('UTF-32BE', 'UTF-8', pack('N', 602)));

Upvotes: 1

edwardsmarkf
edwardsmarkf

Reputation: 1417

 echo json_decode(sprintf('"\u%04x"',$val));

this ultimately worked for me, but i would not have found this without the answer from Niet the Dark Absol

Upvotes: 1

edwardsmarkf
edwardsmarkf

Reputation: 1417

normally, when i attempt to answer my own question, some SO wizard comes along and shows me a built-in function that i should have known about. but until that happens, this is all i can think of:

 $leading_zeros = null;

 if  ( strlen(strval(dechex($val))) >= 4 )       {
     $leading_zeros = '';
 } else if  ( ctype_alpha(dechex($val)[0]) )     {
     $leading_zeros = '00';
 } else if  ( ctype_digit(dechex($val)[0]) )     {
     $leading_zeros = '0';
 }
 echo json_decode('"' . '\u' . $leading_zeros . dechex($val) . '"' );

EDIT: when trying to something similar for javaScript, the documentation tells me the format is supposed to look like "\u####' four digits. i dont know if this is similar to PHP or not.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

While eval is generally to be avoided, it seems strictly-controlled enough to be fine here.

echo eval(sprintf('return "\u{%x}";',$val));

Upvotes: 1

Related Questions