首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP mb_convert_encoding 字串編碼轉換完整教學

     編輯:PHP 綜合知識

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 擴充套件。
  • 編碼名稱大小寫不敏感。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved