How to encode a URL contains Unicode characters with PHP

I'm currently trying to find a url coding solution that contains Unicode, Khmer Unicode characters. I tried using the built-in PHP function urlencode () and gives the result: For example: http://www.example.com/?kwd=Mac+Book+Pro + αž“αŸ… αž”αŸ’αžšαž‘αŸαžŸ αž™αžΎαž„

While I tested with a Google search, this leads to: 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

How to do it? Hope someone here helps me. Thanks in advance!

+5
source share
3 answers

For UTF-8 you can use:

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

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

For UTF-16, you can use this function (from the notes for "urlencode" at http://php.net ):

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;
 }
+9
source
function cleanUrl($url) {
    $res= urlencode(utf8_encode($url));
    $res = str_replace("%3A",":",$res);
    $res = str_replace("%2F","/",$res);
    return $res;
}
0
source

All Articles