栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检测编码并制作一切UTF-8

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检测编码并制作一切UTF-8

如果应用

utf8_enpre()
到已经存在的UTF-8字符串,它将返回乱码的UTF-8输出。

我做了一个解决所有这些问题的功能。叫做

Encoding::toUTF8()

您不需要知道字符串的编码是什么。它可以是Latin1(ISO8859-1),Windows-1252或UTF-8,或者字符串可以混合使用。

Encoding::toUTF8()
会将所有内容转换为UTF-8。

我之所以这样做,是因为某项服务使我的数据馈送全乱了,在同一字符串中混合了UTF-8和Latin1。

用法:

require_once('Encoding.php');use ForceUTF8Encoding;  // It's namespaced now.$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string);$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string);

下载:

https://github.com/neitanod/forceutf8

我提供了另一个函数,

Encoding::fixUFT8()
它将修复每个看起来乱码的UTF-8字符串。

用法:

require_once('Encoding.php');use ForceUTF8Encoding;  // It's namespaced now.$utf8_string = Encoding::fixUTF8($garbled_utf8_string);

例子:

echo Encoding::fixUTF8("Fédération Camerounaise de Football");echo Encoding::fixUTF8("Fédération Camerounaise de Football");echo Encoding::fixUTF8("FÃÂédÃÂération Camerounaise de Football");echo Encoding::fixUTF8("Fédération Camerounaise de Football");

将输出:

Fédération Camerounaise de FootballFédération Camerounaise de FootballFédération Camerounaise de FootballFédération Camerounaise de Football

我已将函数(

forceUTF8
)转换为称为的类上的一系列静态函数
Encoding
。新功能是
Encoding::toUTF8()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号