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 次观看

开启 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.