0

I want to add a google adwords ID as subid to my affiliate link. The problem is that my affiliate program doesn't allow such long subIDs. Is there a way to make it shorter?

For example I get an ID from google that looks like this:

jdfJHGsds57JHJsdkjjkskdfj324GFGHJ3334GHJ

So my link will look like this:

www.mysite.com/?affofferid=jdfJHGsds57JHJsdkjjkskdfj324GFGHJ3334GHJ

I want to make the google ID shorter by encoding or any other method. How can I do this?

I want to be able to convert the ID back to it's original manually later.

Mokka
  • 9
  • 3
  • 1
    https://softwareengineering.stackexchange.com/questions/119181/what-type-of-encoding-can-i-use-to-make-a-string-shorter – Harsha Jayamanna Nov 08 '17 at 11:29
  • 1
    Divide the string to two variables and add it to the url as two different url paramenters. And add them to a single string later. – Gunaseelan Nov 08 '17 at 11:29

1 Answers1

0

Take a look at this post: Shortest possible encoded string with decode possibility (shorten url) using only PHP

He is able to encode a 39-character string into a 30-character one and decode it again.

Here is the encoding function:

function compress($input, $ascii_offset = 38){
    $input = strtoupper($input);
    $output = '';
    //We can try for a 4:3 (8:6) compression (roughly), 24 bits for 4 chars
    foreach(str_split($input, 4) as $chunk) {
        $chunk = str_pad($chunk, 4, '=');
        $int_24 = 0;
        for($i=0; $i<4; $i++){
            //Shift the output to the left 6 bits
            $int_24 <<= 6;
            //Add the next 6 bits
            //Discard the leading ascii chars, i.e make
            $int_24 |= (ord($chunk[$i]) - $ascii_offset) & 0b111111;
        }
        //Here we take the 4 sets of 6 apart in 3 sets of 8
        for($i=0; $i<3; $i++) {
            $output = pack('C', $int_24) . $output;
            $int_24 >>= 8;
        }
    }
    return $output;
}

And the decoding function:

function decompress($input, $ascii_offset = 38) {
    $output = '';
    foreach(str_split($input, 3) as $chunk) {
        //Reassemble the 24 bit ints from 3 bytes
        $int_24 = 0;
        foreach(unpack('C*', $chunk) as $char) {
            $int_24 <<= 8;
            $int_24 |= $char & 0b11111111;
        }
        //Expand the 24 bits to 4 sets of 6, and take their character values
        for($i = 0; $i < 4; $i++) {
            $output = chr($ascii_offset + ($int_24 & 0b111111)) . $output;
            $int_24 >>= 6;
        }
    }
    //Make lowercase again and trim off the padding.
    return strtolower(rtrim($output, '='));
}

He is also proposing a way to keep this information safe during the process by encrypting and decrypting the data.

Here is the full code that calls the aforementioned functions:

$method = 'AES-256-CBC';
$secret = base64_decode('tvFD4Vl6Pu2CmqdKYOhIkEQ8ZO4XA4D8CLowBpLSCvA=');
$iv = base64_decode('AVoIW0Zs2YY2zFm5fazLfg==');
$input = 'img=/dir/dir/hi-res-img.jpg&w=700&h=500';
var_dump($input);
$compressed = compress($input);
var_dump($compressed);
$encrypted = openssl_encrypt($compressed, $method, $secret, false, $iv);
var_dump($encrypted);
$decrypted = openssl_decrypt($encrypted, $method, $secret, false, $iv);
var_dump($decrypted);
$decompressed = decompress($compressed);
var_dump($decompressed);

You could give it a try.

drkostas
  • 517
  • 1
  • 7
  • 29