PHP – 使用 iconv() 將字串轉換為請求的字元編碼


在 PHP 中,iconv() 函式用於將字串轉換為請求的字元編碼。它用於對字串 "string"from_encoding  to_encoding 執行字元集轉換。

語法

string iconv(str $from_encoding, str $to_encoding, str $string)

引數

iconv() 函式接受三個引數:$from_encoding$to_encoding$string

  • $from_encoding− 此引數用於指定輸入字元集。

  • $to_encoding− 此引數用於輸出字元集。

  • $string− 此引數用於轉換字串。

返回值

iconv() 在成功時返回轉換後的字串,失敗時返回 False。

示例

 演練

<pre>
   <?php
      // used the Dollar symbol to convert in string
      $text = "the Dollar symbol '$'";

      echo 'Original:', $text, PHP_EOL;
      echo 'TRANSLIT: ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
      echo 'IGNORE: ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
   ?>
</pre>

輸出

Original:the Dollar symbol '$'
TRANSLIT: the Dollar symbol '$'
IGNORE: the Dollar symbol '$'

示例 2

 演練

<pre>
   <?php
      // used the Dollar symbol to convert in string
      $string = "Indian Rupees '?'";

      echo 'Original: ', $string, PHP_EOL;
      echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1", $string), PHP_EOL;
      echo 'IGNORE: ', iconv("UTF-8", "ISO-8859-1", $string), PHP_EOL;
   ?>
</pre>

輸出

Original: Indian Rupees '?'
TRANSLIT: Indian Rupees '?'
IGNORE: Indian Rupees '?'

更新時間:2021 年 8 月 23 日

946 次觀看

開啟 職業生涯

完成課程,獲得認證

開始
廣告