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 '?'
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP