PHP如何把字符串改成UTF8?-PHP问题

资源魔 28 0

PHP若何把字符串改为UTF8?

正在PHP中能够应用“iconv()”函数把字符串改为UTF8,该函数作用是将字符串按要求的字符编码来转换,其语法为“iconv(in,out,str)”,应用时将in设置为字符串的编码,out设置为UTF8,str设置为要转换的字符串便可。

转化示例

$str = "123456789";

$str = iconv('ASCII', 'UTF8', $str);

应用示例

<?php
//some German
$utf8_sentence = 'Weiß, Goldmann, Göbel, Weiss, Göthe, Goethe und Götz';

//UK
setlocale(LC_ALL, 'en_GB');

//transliterate
$trans_sentence = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_sentence);

//gives [Weiss, Goldmann, Gobel, Weiss, Gothe, Goethe und Gotz]
//which is our original string flattened into 7-bit ASCII as
//an English speaker would do it (ie. simply remove the umlauts)
echo $trans_sentence . PHP_EOL;

//Germany
setlocale(LC_ALL, 'de_DE');

$trans_sentence = iconv('UTF-8', 'ASCII//TRANSLIT', $utf8_sentence);

//gives [Weiss, Goldmann, Goebel, Weiss, Goethe, Goethe und Goetz]
//which is exactly how a German would transliterate those
//umlauted characters if forced to use 7-bit ASCII!
//(because really ä = ae, ö = oe and ü = ue)
echo $trans_sentence . PHP_EOL;

?>
<?php
$tab = array("UTF-8", "ASCII", "Windows-1252", "ISO-8859-15", "ISO-8859-1", "ISO-8859-6", "CP1256");
$chain = "";
foreach ($tab as $i)
    {
        foreach ($tab as $j)
        {
            $chain .= " $i$j ".iconv($i, $j, "$my_string");
        }
    }

echo $chain;
?>

保举教程:《PHP教程》

以上就是PHP若何把字符串改为UTF8?的具体内容,更多请存眷资源魔其它相干文章!

标签: php教程 php故障解决 php使用问题 PHP基础

抱歉,评论功能暂时关闭!