php字符串轉(zhuǎn)utf8編碼的方法:首先使用“mb_detect_encoding”方法自動識別字符串編碼;然后通過“mb_convert_encoding”函數(shù)將其轉(zhuǎn)換成國際標準編碼“utf-8”即可。
推薦:《PHP視頻教程》
在使用mb_convert_encoding時要先知道字符編碼,如果編碼錯誤就會亂碼,使用mb_detect_encoding自動識別字符串編碼,并轉(zhuǎn)換成國際標準編碼utf-8編碼。
<?php $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5','LATIN1')); if($encode != 'UTF-8'){ $name = mb_convert_encoding($name, 'UTF-8', $encode); }
mb_convert_encoding — 轉(zhuǎn)換字符的編碼
mb_detect_encoding — 檢測字符的編碼