php如何轉換字元編碼爲utf8

2020-08-11 18:01:06

php轉換字元編碼爲utf8的方法:首先利用mb_detect_encoding()函數找出字串本身的編碼;然後利用mb_convert_encoding()函數進行編碼轉換即可。

mb_convert_encoding()函數語法:

(推薦教學:)

mb_convert_encoding( $str, $encoding1,$encoding2 );

參數:

  • $str,要轉換編碼的字串

  • $encoding1,目標編碼,如utf-8,gbk,大小寫均可

  • $encoding2,原編碼,如utf-8,gbk,大小寫均可

(視訊教學推薦:)

思路:

先找出字串本身的編碼,再轉換爲utf-8編碼。

程式碼實現:

function str_to_utf8 ($str = '') {
    $current_encode = mb_detect_encoding($str, array("ASCII","GB2312","GBK",'BIG5','UTF-8')); 
    $encoded_str = mb_convert_encoding($str, 'UTF-8', $current_encode);
    return $encoded_str;
}

以上就是php如何轉換字元編碼爲utf8的詳細內容,更多請關注php中文網其它相關文章!