首頁  >  文章  >  後端開發  >  php字串如何轉utf8編碼

php字串如何轉utf8編碼

藏色散人
藏色散人原創
2020-09-07 09:52:272992瀏覽

php字串轉utf8編碼的方法:首先使用「mb_detect_encoding」方法自動識別字串編碼;然後透過「mb_convert_encoding」函數轉換成國際標準編碼「utf-8」即可。

php字串如何轉utf8編碼

推薦:《PHP影片教學

在使用mb_convert_encoding時要先知道字元編碼,如果編碼錯誤就會亂碼,使用mb_detect_encoding自動辨識字串編碼,並轉換成國際標準編碼utf-8編碼。

<?php
$encode = mb_detect_encoding($str, array("ASCII",&#39;UTF-8&#39;,"GB2312","GBK",&#39;BIG5&#39;,&#39;LATIN1&#39;));
if($encode != &#39;UTF-8&#39;){
    $name = mb_convert_encoding($name, &#39;UTF-8&#39;, $encode);
}

mb_convert_encoding — 轉換字元的編碼

mb_detect_encoding — 偵測字元的編碼

以上是php字串如何轉utf8編碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn