3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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";
Output for 5.0.0 - 5.5.34, 5.6.7 - 5.6.20, 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.0.5, 7.0.14 - 7.1.0
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 \u8c46\u7c7b a
Output for 7.0.6
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 Fatal error: Uncaught Error: Call to undefined function iconv() in /in/IHo4t:16 Stack trace: #0 {main} thrown in /in/IHo4t on line 16
Process exited with code 255.
Output for 5.5.35, 5.6.21
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 Fatal error: Call to undefined function iconv() in /in/IHo4t on line 16
Process exited with code 255.
Output for 4.4.5 - 4.4.9
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 Fatal error: Call to undefined function: iconv() in /in/IHo4t on line 16
Process exited with code 255.
Output for 4.3.2 - 4.4.4
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 Fatal error: Call to undefined function: iconv() in /in/IHo4t on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1
�%E5%9B%9E回UTF-8编码: E59B9E Unicode编码: 22238 Fatal error: Call to undefined function: iconv() in /in/IHo4t on line 16