mrpatg
mrpatg

Reputation: 10117

url shortener modification

Im working with a php url shortener and my problem is that it only creates the MAX number of shortening codes up to 2 characters (eg domain.com/XX). I want it to go up to 5 characters (eg domain.com/XXXXX)

I believe i found the relevant code, but im not sure how to change it to allow for this modification

function decode_url_id($code)
    {   
        $scheme = "abcdefghijklmnoprstuqwxvyz0123456789ABCDEFGHIJKLMNOPRSTQWXUVYZ";
        $scheme_size = strlen($scheme);

        $number  = 0;
        $code_size = strlen($code);
        $code = strrev($code);
        for($i = 0; $i < $code_size; $i++)
        {
            $digit_value = strpos($scheme, $code[$i]);

            $number += ($digit_value * pow($scheme_size, $i));
        }

        return $number;
    }

    function encode_url_id($number, $code="")
    {
        $scheme = "abcdefghijklmnoprstuqwxvyz0123456789ABCDEFGHIJKLMNOPRSTQWXUVYZ";
        $scheme_size = strlen($scheme);

        if ($number >= $scheme_size)
        {
            $c = $number % $scheme_size;
            $code .= $scheme[$c];
            $number = floor($number / $scheme_size);

            return encode_url_id($number, $code);
        }
        else 
        {
            $code .= $scheme[$number];
            $code = strrev($code);
        }

        return $code;
    }

Am i barking up the wrong tree?

Upvotes: 0

Views: 240

Answers (1)

James Simpson
James Simpson

Reputation: 13718

Why not just encode the ID of the URL in the database with http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps.

Example usage:

$new_url = base_base2base($link_id, 10, 62);

Upvotes: 2

Related Questions