1

I am making a dynamic Unicode icon in PHP. I want the UTF-8 code of the Unicode icon.

So far I have done:

$value = "1F600";
$emoIcon = "\u{$value}";

$emoIcon = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $emoIcon);
echo $emoIcon; //output 😀
$hex=bin2hex($emoIcon);
echo $hex;  // output 26237831463630303b
$hexVal=chunk_split($hex,2,"\\x");
var_dump($hexVal);  // output  26\x23\x78\x31\x46\x36\x30\x30\x3b\x
$result= "\\x" . substr($hexVal,0,-2);
var_dump($result);    // output  \x26\x23\x78\x31\x46\x36\x30\x30\x3b

But when I put the value directly, it prints the correct data:

$emoIcon = "\u{1F600}";

$emoIcon = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $emoIcon);
echo $emoIcon; //output 😀
$hex=bin2hex($emoIcon);
echo $hex;  // output f09f9880
$hexVal=chunk_split($hex,2,"\\x");
var_dump($hexVal);  // output  f0\x9f\x98\x80\x
$result= "\\x" . substr($hexVal,0,-2);
var_dump($result);    // output  \xf0\x9f\x98\x80
1
  • Please clarify your question. What do you want to achieve? Commented Jul 29, 2017 at 14:32

1 Answer 1

6

\u{1F600} is a Unicode escape sequence used in double-quoted strings, it must have a literal value - trying to use "\u{$value}", as you've seen, doesn't work (for a couple reasons, but that doesn't matter so much.)

If you want to start with "1F600" and end up with 😀 use hexdec to turn it into an integer and feed that to IntlChar::chr to encode that code point as UTF-8. E.g.:

$value = "1F600";
echo IntlChar::chr(hexdec($value));

Outputs:

😀
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.