有人可以解释为什么输出是ASCII在下面的最后三个测试中?
我在自己的系统,PHPTester.net和PhpFiddle.org上得到了相同的结果.
echo mb_internal_encoding(); // UTF-8
$str = 'foobar';
echo mb_check_encoding($str, 'UTF-8'); // true
echo mb_detect_encoding($str); // ASCII
$encoded = utf8_encode($str);
echo mb_detect_encoding($encoded); // ASCII
$converted = mb_convert_encoding($str, 'UTF-8');
echo mb_detect_encoding($converted); // ASCII
Run Code Online (Sandbox Code Playgroud)
那是因为没有字符foobar不能用ASCII表示.
mb_check_encoding($str, 'UTF-8') 是因为ASCII文本与UTF-8天生兼容(故意这样)
但是在没有多字节字符的情况下,两者之间没有明显的区别.证明:'foobar' === utf8_encode('foobar') // true