當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP mb_convert_encoding()用法及代碼示例

mb_convert_encoding() 函數是 PHP 中的內置函數,可將字符串轉換為另一種字符編碼。

用法:

mb_convert_encoding(
    array|string $string, 
    string $to_encoding, 
    array|string|null 
    $from_encoding = null
): array|string|false

Parameters: 該函數有3個參數:

  • string: 參數指定要轉換的字符串或數組。
  • to_encoding: 該參數指定結果所需的編碼。
  • from_encoding: 如果mbstring.internal_encoding將使用設置,如果from_encoding為空或未指定,否則default_charset將使用設置。

返回值:如果函數執行成功,該函數將返回一個編碼字符串,否則將返回 false。

示例 1:下麵的代碼演示了mb_convert_encoding()函數。

PHP


<?php 
  
// UTF-8 string to be converted to ASCII 
$string = "Café au lait"; 
  
// Convert UTF-8 string to ASCII 
$ascii_string = mb_convert_encoding($string, "ASCII"); 
  
// Output the converted string 
echo $ascii_string; 
?>

輸出:

Caf? au lait  

示例 2:下麵的代碼演示了mb_convert_encoding()函數。

PHP


<?php 
  
// ISO-8859-1 string to be converted to UTF-8 
$string = "Björk"; 
  
// Convert ISO-8859-1 string to UTF-8 
$utf8_string = mb_convert_encoding( 
    $string, "UTF-8", "ISO-8859-1"); 
  
// Output the converted string 
echo $utf8_string; 
?>

輸出:

Björk       

參考: https://www.php.net/manual/en/function.mb-convert-encoding.php



相關用法


注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 PHP mb_convert_encoding() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。