我試圖檢測字符串的字符編碼,但我無法得到正確的結果。
例如:在PHP中檢測正確的字符編碼?
$str = "€ ‚ ƒ „ …" ;
$str = mb_convert_encoding($str, 'Windows-1252' ,'HTML-ENTITIES') ;
// Now $str should be a Windows-1252-encoded string.
// Let's detect its encoding:
echo mb_detect_encoding($str,'Windows-1252, ISO-8859-1, UTF-8') ;
即代碼輸出ISO-8859-1
但它應該是Windows-1252
。
這是怎麼回事?
編輯:
更新的示例,以迴應@ raina77ow。
$str = "€‚ƒ„…" ; // no white-spaces
$str = mb_convert_encoding($str, 'Windows-1252' ,'HTML-ENTITIES') ;
$str = "Hello $str" ; // let's add some ascii characters
echo mb_detect_encoding($str,'Windows-1252, ISO-8859-1, UTF-8') ;
我再次得到錯誤的結果。
什麼是您使用的PHP版本?它在這裏顯示正確http://codepad.viper-7.com/NfvdWm。順便說一句,你應該總是首先列出UTF-8,因爲它是最受限制的。可能根本不需要ISO-8859-1,因爲任何東西都是有效的ISO-8859-1。 – Esailija 2013-04-06 09:25:55
@Esailija,你沒有使用問題中任何兩個例子的代碼。試試這兩個例子中的任何一個,你就會得到結果'ISO-8859-1'。 – GetFree 2013-04-07 01:17:10