iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⛳
[PHP] Converting between code points and characters via OS commands
I tried generating characters from code points using OS commands via proc_open. Since PHP 7.4, it is possible to pass commands as an array.
$c = sprintf('\\u%x', 0x3042);
$cmd = ['printf', '%b', $c];
$cmd2 = 'printf 🐶 | uconv -x "Any-Hex/Unicode"';
echo prop_exec($cmd), PHP_EOL;
echo prop_exec($cmd2), PHP_EOL;
function prop_exec($cmd) {
$descriptorspec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w']
];
$process = proc_open($cmd, $descriptorspec, $pipes);
return stream_get_contents($pipes[1]);
proc_close($process);
}
To install uconv on Debian, you can install the icu-devtools package.
sudo apt install icu-devtools
You can also find the code point using shell_exec.
$cmd = 'printf 🐶 | uconv -x "Any-Hex/Unicode"';
echo shell_exec("($cmd)"),PHP_EOL;
You can also generate characters from code points using eval without using OS commands.
$c = sprintf('\\u{%x}', 0x3042);
$cmd = 'echo "' . $c . '";';
ob_start();
eval($cmd);
$ret = ob_get_clean();
echo $ret, PHP_EOL;
Discussion