Moak
Moak

Reputation: 12885

URLencode numbers?

I want to encode this

广东路20号外滩5号7楼,近中山东一路 into this:

%e5%b9%bf%e4%b8%9c%e8%b7%af%32%30%e5%8f%b7%e5%a4%96%e6%bb%a9%35%e5%8f%b7%37%e6%a5%bc%ef%bc%8c%e8%bf%91%e4%b8%ad%e5%b1%b1%e4%b8%9c%e4%b8%80%e8%b7%af
however urlencode() doesn't seem to encode numbers and the api I need to use breaks at 20

--------------------------v
%E5%B9%BF%E4%B8%9C%E8%B7%AF20%E5%8F%B7%E5%A4%96%E6%BB%A95%E5%8F%B77%E6%A5%BC%EF%BC%8C%E8%BF%91%E4%B8%AD%E5%B1%B1%E4%B8%9C%E4%B8%80%E8%B7%AF

How can I encode alphanumeric characters to the target format?

From the answer I've created this function

/**
 * urlencodes complete string, including alphanumeric characters
 * @param string $string the string to encode
 */
function urlencode_all($string){
    $chars = array();
    for($i = 0; $i < strlen($string); $i++){
        $chars[] = '%'.dechex(ord($string[$i]));
    }
    return implode('', $chars);
}

Upvotes: 0

Views: 2030

Answers (1)

PiTheNumber
PiTheNumber

Reputation: 23552

urlencode() works this way. It does not need to convert numbers. If you like to convert everything you can do that on your own. Try

$s = '广东路20号外滩5号7楼,近中山东一路';
for($i = 0; $i < strlen($s); $i++)
{
  echo '%' . dechex(ord($s[$i]));
}

http://codepad.org/bvRU8gQv

Upvotes: 2

Related Questions