在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';//uksetlocale(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;//germanysetlocale(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?的详细内容。
