2012-03-28 104 views
5

目前,我正在尋找解決方案來編碼包含unicode字符,高棉Unicode的url。我已經使用PHP內置函數urlencode()來嘗試,它給結果: 例如:http://www.example.com/?kwd=Mac+Book+Pro +នៅប្រទេសយើង如何編碼URL包含Unicode字符與PHP

雖然我已經與谷歌搜索進行測試,它的結果: https://www.google.com.kh/#hl=en&sclient=psy-ab&q=Mac+Book+Pro+%E1%9E%93%E1%9F%85%E1%9E%94%E1%9F%92%E1%9E%9A%E1%9E%91%E1%9F%81%E1%9E%9F%E1%9E%99%E1%9E%BE%E1%9E%84&oq=Mac+Book+Pro+%E1%9E%93%E1%9F%85%E1%9E%94%E1%9F%92%E1%9E%9A%E1%9E%91%E1%9F%81%E1%9E%9F%E1%9E%99%E1%9E%BE%E1%9E%84

怎麼辦那?希望這裏有人能幫助我。 在此先感謝!

回答

9

爲UTF-8,你可以使用:

urlencode(utf8_encode($string)); //for encoding 

utf8_decode(urldecode($string)); //for decoding 

對於UTF-16,您可以使用此功能(從筆記中http://php.net 「進行urlencode」):

function utf16_urlencode ($str) { 
    # convert characters > 255 into HTML entities 
    $convmap = array(0xFF, 0x2FFFF, 0, 0xFFFF); 
    $str = mb_encode_numericentity($str, $convmap, "UTF-8"); 

    # escape HTML entities, so they are not urlencoded 
    $str = preg_replace('/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str); 
    $str = urlencode($str); 

    # now convert escaped entities into unicode url syntax 
    $str = preg_replace('/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str); 
    return $str; 
} 
+0

感謝Andy,其實我已經試過了上面的代碼:urlencode(utf8_encode($ string)); //對於編碼,但它仍然不會產生與我的問題中所述的Google相同的結果。我甚至嘗試過這個函數:utf16_urlencode(),但它不會產生我想要的。謝謝你 – Thavarith 2012-03-28 05:11:24

0
function cleanUrl($url) { 
    $res= urlencode(utf8_encode($url)); 
    $res = str_replace("%3A",":",$res); 
    $res = str_replace("%2F","/",$res); 
    return $res; 
} 
相關問題