mb_convert_encoding 用於字串編碼轉換,支援指定來源編碼或自動偵測。
mb_convert_encoding($str, $to_encoding, $from_encoding)
$str:待轉換字串$to_encoding:目標編碼(如 UTF-8, GBK)$from_encoding:來源編碼,省略或 "auto" 為自動偵測<?php
$str = '編碼';
// 1. 僅指定目標編碼(自動偵測來源)
echo mb_convert_encoding($str, "UTF-8");
// 2. 指定來源為 GBK
echo mb_convert_encoding($str, "UTF-8", "GBK");
// 3. 明確自動偵測
echo mb_convert_encoding($str, "UTF-8", "auto");
?>
mbstring 擴充套件。