使用php获取网页源代码时会出现乱码的情况,使用mb_convert_encoding转码即可。

$contents= file_get_contents('http://www.ecjosn.com/'); // 获取 页面内容  
$en_contents=mb_convert_encoding($contents, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');  // 对页面内容进行编码

mb_convert_encoding的使用:

    方法原型:string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )

    将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。

参数:

    str:要编码的 string

    to_encoding:要转换成的编码类型。

    from_encoding:转换前通过字符代码名称来指定。它可以是一个 array 也可以是逗号分隔的枚举列表。 如果没有提供from_encoding,则会使用内部(internal)编码。