php把字符串改成utf8,PHP将字符串改成UTF8的方法

PHP将字符串改成UTF8的方法

发布时间:2020-06-15 22:22:25

来源:亿速云

阅读:82

作者:元一

UTF-8是什么?

UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,也是一种前缀码,又称万国码。由Ken Thompson于1992年创建。它可以用来表示Unicode标准中的任何字符,且其编码中的第一个字节仍与ASCII兼容,这使得原来处理ASCII字符的软件无须或只须做少部份修改,即可继续使用。因此,它逐渐成为电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。

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;

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值