How do I detect if have to apply UTF-8 decode or encode on a string?(如何检测是否必须对字符串应用 UTF-8 解码或编码?)
问题描述
我有从第三方网站获取的提要,有时我必须应用 utf8_decode
和其他时候 utf8_encode
以获得所需的可见输出.
I have a feed taken from third-party sites, and sometimes I have to apply utf8_decode
and other times utf8_encode
to get the desired visible output.
如果错误地应用相同的东西两次/或使用了错误的方法,我会得到更丑陋的东西,这就是我想要改变的.
If by mistake the same stuff is applied twice/or the wrong method is used I get something more ugly, this is what I want to change.
如何检测何时必须对字符串应用什么?
How can I detect when what have to apply on the string?
其实内容返回的是UTF-8,但里面有部分不是.
Actually the content returns UTF-8, but inside there are parts that are not.
推荐答案
我不能说我可以依赖 mb_detect_encoding()
.不久前我遇到了一些奇怪的误报.
I can't say I can rely on mb_detect_encoding()
. I had some freaky false positives a while back.
我发现在每种情况下都能很好地工作的最普遍的方法是:
The most universal way I found to work well in every case was:
if (preg_match('!!u', $string))
{
// This is UTF-8
}
else
{
// Definitely not UTF-8
}
这篇关于如何检测是否必须对字符串应用 UTF-8 解码或编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测是否必须对字符串应用 UTF-8 解码或编码?


- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01