1

i am confuse about Unicode codepoint escape syntax. here is a demo

//this work fine 
echo "\u{1f602}"; // echoes 
//this doesn't work
$var = '1f602';
echo '"\u{' . $var . '}"';// out put \u1f602

after i search. i find eval will let it work correct

$var = '1f602';
eval('$re = "\u{' . $var . '}";');
echo $re; //echoes 

so i wanna to know the reason, what cause this? how internal of php process it? not support on the runtime? thanks!!!

  • thanks, i think it is not single quoted or double quoted problem. i has try it both. Neither works. @Ivar – miracle00001 Mar 14 '21 at 11:24
  • Right. It does matter (it will never work using single quotes), but that's only part of the problem. – Ivar Mar 14 '21 at 11:31
  • The first one matches `\u{[0-9A-Fa-f]+}`, the second doesn't. Also single quotes won't be parsed. `"\u{" . $var . "}"` would be closer but still doesn't match. https://www.php.net/manual/en/language.types.string.php – user3783243 Mar 14 '21 at 11:54
  • You can't do it with the double quote expansion. The question is about `why` not `how`, right? For how see https://stackoverflow.com/questions/48432078/php-unicode-codepoint-to-character – user3783243 Mar 14 '21 at 12:09
  • thanks, i wanna to know why, why eval will let it work? or say why "\u{" . $var . "}" not complies with [the \u{} syntax](https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.unicode-codepoint-escape-syntax) @user3783243 – miracle00001 Mar 14 '21 at 12:32
  • The PHP expands the `$var` first to run the `eval` so you end up with `$re = "\u{1f602}";` which will run natively in PHP because it matches the regex the double quote expander uses. I don't know how to do it without the `eval` well using the double quote expansion. – user3783243 Mar 14 '21 at 12:43
  • @user3783243 : how u test it whether matches the regex the double quote expander uses? sorry, what is double quote expander, something like php Interpreter? i guess the \u{} syntax only work in compile, not in runtime, i doesn't have enough knowledge to know if my guess is correct – miracle00001 Mar 14 '21 at 13:43
  • See https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double that is what converts it to the emoji, also what converts variables to value. – user3783243 Mar 14 '21 at 21:21

0 Answers0