Convert MD5 to base62 for URL

前端 未结 9 507
再見小時候
再見小時候 2020-12-30 10:43

I have a script to convert to base 62 (A-Za-z0-9) but how do I get a number out of MD5?

I have read in many places that because the number from an MD5 is bigger than

9条回答
  •  难免孤独
    2020-12-30 11:25

    You could use a slightly modified Base 64 with - and _ instead of + and /:

    function base64_url_encode($str) {
        return strtr(base64_encode($str), array('+'=>'-', '/'=>'_'));
    }
    function base64_url_decode($str) {
        return base64_decode(strtr($str, array('-'=>'+', '_'=>'/')));
    }
    

    Additionally you could remove the trailing padding = characters.

    And to get the raw MD5 value (binary string), set the second parameter (named $raw_output in the manual) to true:

    $raw_md5 = md5($str, true);
    

提交回复
热议问题