目前,我正在寻找解决方案来编码包含unicode字符,高棉Unicode的url。我已经使用PHP内置函数urlencode()来尝试,它给结果: 例如:http://www.example.com/?kwd=Mac+Book+Pro +នៅប្រទេសយើង如何编码URL包含Unicode字符与PHP
怎么办那?希望这里有人能帮助我。 在此先感谢!
目前,我正在寻找解决方案来编码包含unicode字符,高棉Unicode的url。我已经使用PHP内置函数urlencode()来尝试,它给结果: 例如:http://www.example.com/?kwd=Mac+Book+Pro +នៅប្រទេសយើង如何编码URL包含Unicode字符与PHP
怎么办那?希望这里有人能帮助我。 在此先感谢!
为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;
}
尝试rawurlencode
function cleanUrl($url) {
$res= urlencode(utf8_encode($url));
$res = str_replace("%3A",":",$res);
$res = str_replace("%2F","/",$res);
return $res;
}
感谢Andy,其实我已经试过了上面的代码:urlencode(utf8_encode($ string)); //对于编码,但它仍然不会产生与我的问题中所述的Google相同的结果。我甚至尝试过这个函数:utf16_urlencode(),但它不会产生我想要的。谢谢你 – Thavarith 2012-03-28 05:11:24