2012-10-11 38 views
1

我爲我的項目使用第三方授權服務。此服務返回我的名字,如下所示:u0438u0432u0430u043d,但我的名字是 - иван。如何將編碼值(u0438u0432u0430u043d)轉換爲PHP中的иван? 謝謝!使用php進行未知的編碼和編碼轉換使用php

+0

這些都是錯位的Unicode序列。他們應該看起來像'\ u0438 \ u0432etc ...'。 –

+0

是的 - 正確的\ u0438 \ u0432 ... – user889349

回答

1

你已經有了統一的十六進制序列存在 - 試圖適應這樣的事情...

function decodeToUTF8($s) { 
    //turn udddd into an HTML entity 
    $s=preg_replace('/u(....)/', '&#x$1;', $s); 
    //turn entities into UTF-8 
    return html_entity_decode($s, ENT_COMPAT, 'utf-8'); 
} 
+0

它的工作!謝謝。 – user889349

-1

這是正確的解決方案:

protected static function decodeToUTF8($s) { 
     //turn udddd into an HTML entity 
     $s=preg_replace('/u([A-Fa-f0-9]{4})/', '&#x$1;', $s); 
     //turn entities into UTF-8 
     return html_entity_decode($s, ENT_COMPAT, 'utf-8'); 
    }