小编Dak*_*wal的帖子

在 PHP 中从动态 Unicode 创建 UTF-8 代码

我正在用 PHP 制作一个动态 Unicode 图标。我想要 Unicode 图标的 UTF-8 代码。

到目前为止我已经做了:

$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
Run Code Online (Sandbox Code Playgroud)

但是当我直接输入值时,它会打印正确的数据:

$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
Run Code Online (Sandbox Code Playgroud)

php unicode utf-8

1
推荐指数
1
解决办法
463
查看次数

标签 统计

php ×1

unicode ×1

utf-8 ×1