3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = '回';echo $str{0};echo urlencode($str); echo $bin = pack("C3", ord($str{0}), ord($str{1}), ord($str{2}));//回 $hex = strtoupper(bin2hex($bin)); echo "UTF-8编码: " . $hex . "\n"; $byte1 = ord($str{0}); $byte2 = ord($str{1}); $byte3 = ord($str{2}); $c1 = (($byte1 & 0x0F) << 4) | (($byte2 & 0x3F) >> 2); $c2 = (($byte2 & 0x03) << 6) | ($byte3 & 0x3F); $dec = (($c1 & 0x00FF) << 8) | $c2; echo "Unicode编码: " . $dec . "\n"; $str = "\u00b6\u00b9\u00c0\u00e0"; $gbk_str = pack("H*", str_replace("\u00", "", $str)); $utf8_str = iconv("GBK", "UTF-8//IGNORE", $gbk_str); $utf8_len = strlen($utf8_str); $res = ""; for ($i = 0; $i < $utf8_len; $i++) { $byte1 = ord($utf8_str{$i}); // UTF-8是变长字节,占1~6字节。 // 这里是固定情况,就不作这么多判断了 // 直接当三字节 if ((($byte1 >> 4) & 0xFF) == 0xE) { // U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx $byte2 = ord(substr($utf8_str, ++$i, 1)); $byte3 = ord(substr($utf8_str, ++$i, 1)); $b1 = ($byte1 << 4) | (($byte2 >> 2) & 0x0F); $b2 = (($byte2 & 0x03) << 6) | ($byte3 & 0x3F); $unicode = (($b1 & 0x00FF) << 8) | $b2; $res .= sprintf("\u%x", $unicode); } else { // 略过... } } echo $res . "\n"; // output: \u8c46\u7c7b echo pack("C", 97) . "\n";

preferences:
39.49 ms | 402 KiB | 5 Q