mb_detect_order('GBK, UTF-8, ASCII');
$baidu = file_get_contents('http://www.baidu.com');
$youdao = file_get_contents('http://www.youdao.com');
echo mb_detect_encoding($youdao);  
$baidu = mb_convert_encoding($baidu, 'UTF-8', 'GBK');
$youdao = mb_convert_encoding($youdao, 'GBK', 'utf-8');
echo $baidu;
echo $youdao;
上面的代码中,mb_convert_encoding我必须都指定$from_encoding才能得到正确的结果。按照我的理解,如果我没有指定$from_encoding的话,我至少应该有一个得到正确结果吧?结果如果忽略$from_encoding的话,两个都得不到。这是什么原因呢?
手册上这么说:
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used. "auto" may be used, which expands to "ASCII,JIS,UTF-8,EUC-JP,SJIS". 
他说如果没有指明from_encoding的话,就会使用内部编码?什么是内部编码?刚开始我以为是mb_detect_order()指定的。发现不对,然后我又以为是该文件的编码,结果还是不符合预期。难道是操作系统使用的编码?还是不符合我的预期啊。

解决方案 »

  1.   

    这样能正常显示, FF3.5, 我的文件编码是utf-8
    //mb_detect_order('GBK, UTF-8, ASCII');
    $baidu = file_get_contents('http://www.baidu.com');
    $youdao = file_get_contents('http://www.youdao.com');
    //echo mb_detect_encoding($youdao);  
    //$baidu = mb_convert_encoding($baidu, 'UTF-8', 'GBK');
    $youdao = mb_convert_encoding($youdao, 'GBK', 'utf-8');
    echo $baidu;
    echo $youdao;