2010-04-05 169 views
10

有沒有一個PHP函數來處理下面的編碼?將unicode轉換爲特殊字符的PHP函數?

.replaceAll("\u00c3\u0080", "À") 
    .replaceAll("\u00c3\u0081", "Á") 
    .replaceAll("\u00c3\u0082", "Â") 
    .replaceAll("\u00c3\u0083", "Ã") 
    .replaceAll("\u00c3\u0084", "Ä") 
    .replaceAll("\u00c3\u0085", "Å") 
    .replaceAll("\u00c3\u0086", "Æ") 
    .replaceAll("\u00c3\u00a0", "à") 
    .replaceAll("\u00c3\u00a1", "á") 
    .replaceAll("\u00c3\u00a2", "â") 
    .replaceAll("\u00c3\u00a3", "ã") 
    .replaceAll("\u00c3\u00a4", "ä") 
    .replaceAll("\u00c3\u00a5", "å") 
    .replaceAll("\u00c3\u00a6", "æ") 
    .replaceAll("\u00c3\u0087", "Ç") 
    .replaceAll("\u00c3\u00a7", "ç") 
    .replaceAll("\u00c3\u0090", "Ð") 
    .replaceAll("\u00c3\u00b0", "ð") 
    .replaceAll("\u00c3\u0088", "È") 
    .replaceAll("\u00c3\u0089", "É") 
    .replaceAll("\u00c3\u008a", "Ê") 
    .replaceAll("\u00c3\u008b", "Ë") 
    .replaceAll("\u00c3\u00a8", "è") 
    .replaceAll("\u00c3\u00a9", "é") 
    .replaceAll("\u00c3\u00aa", "ê") 
    .replaceAll("\u00c3\u00ab", "ë") 
    .replaceAll("\u00c3\u008c", "Ì") 
    .replaceAll("\u00c3\u008d", "Í") 
    .replaceAll("\u00c3\u008e", "Î") 
    .replaceAll("\u00c3\u008f", "Ï") 
    .replaceAll("\u00c3\u00ac", "ì") 
    .replaceAll("\u00c3\u00ad", "í") 
    .replaceAll("\u00c3\u00ae", "î") 
    .replaceAll("\u00c3\u00af", "ï") 
    .replaceAll("\u00c3\u0091", "Ñ") 
    .replaceAll("\u00c3\u00b1", "ñ") 
    .replaceAll("\u00c3\u0092", "Ò") 
    .replaceAll("\u00c3\u0093", "Ó") 
    .replaceAll("\u00c3\u0094", "Ô") 
    .replaceAll("\u00c3\u0095", "Õ") 
    .replaceAll("\u00c3\u0096", "Ö") 
    .replaceAll("\u00c3\u0098", "Ø") 
    .replaceAll("\u00c5\u0092", "Œ") 
    .replaceAll("\u00c3\u00b2", "ò") 
    .replaceAll("\u00c3\u00b3", "ó") 
    .replaceAll("\u00c3\u00b4", "ô") 
    .replaceAll("\u00c3\u00b5", "õ") 
    .replaceAll("\u00c3\u00b6", "ö") 
    .replaceAll("\u00c3\u00b8", "ø") 
    .replaceAll("\u00c5\u0093", "œ") 
    .replaceAll("\u00c3\u0099", "Ù") 
    .replaceAll("\u00c3\u009a", "Ú") 
    .replaceAll("\u00c3\u009b", "Û") 
    .replaceAll("\u00c3\u009c", "Ü") 
    .replaceAll("\u00c3\u00b9", "ù") 
    .replaceAll("\u00c3\u00ba", "ú") 
    .replaceAll("\u00c3\u00bb", "û") 
    .replaceAll("\u00c3\u00bc", "ü") 
    .replaceAll("\u00c3\u009d", "Ý") 
    .replaceAll("\u00c5\u00b8", "Ÿ") 
    .replaceAll("\u00c3\u00bd", "ý") 
    .replaceAll("\u00c3\u00bf", "ÿ"); 

回答

14

與「到」編碼嘗試mb_convert_encoding()'HTML-ENTITIES',和(如有必要)「從」編碼設置爲'UTF-8'或者您正在使用哪個Unicode編碼。

+0

作品,謝謝! – rawrrrrrrrr 2010-04-05 10:45:09

+1

在php6中,不再需要MultiByte(MB)。 – confiq 2010-04-05 10:56:34

2

使用strtr,它快速。

+0

不起作用,因爲html實體是多個字符,'strtr()'只能執行1:1字符映射。 – Amber 2010-04-05 10:35:15

+1

這是不正確的,它可以在數組的鍵 - 值對上操作(用'值'替換'key'中的每一個'),您可以在其中替換多字符字符串。 – soulmerge 2010-04-05 10:51:22

2

我發現這個到以前的答案是不工作很有趣,:

function jsonRemoveUnicodeSequences($struct) { 
    return preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($struct)); 
} 

我在這裏找到:http://www.avoid.org/replace-u-characters-in-json-string/