問題描述
我有從第三方網站獲取的提要,有時我必須應用 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 解碼或編碼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!