DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kenneth has posted 3 posts at DZone. View Full User Profile

Shorten / Enlarge A Tweet ID

07.02.2009
| 3906 views |
  • submit to reddit
        Thinking about how to do threaded tweets
%tweet id replying to% .....
where "tweet id replying to" would be the shortened tweet.

<?php
// Set up en/decoder ring
// 65 (index 0 - 64) characters
$enring = array(0,   1,   2,   3,
                4,   5,   6,   7,
                8,   9,
                'a', 'b', 'c', 'd',
                'e', 'f', 'g', 'h',
                'i', 'j', 'k', 'l',
                'm', 'n', 'o', 'p',
                'q', 'r', 's', 't',
                'u', 'v', 'w', 'x',
                'y', 'z',
                'A', 'B', 'C', 'D',
                'E', 'F', 'G', 'H',
                'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X',
                'Y', 'Z',
                '_', '-', '~');

$dering = array(0,         1,         2,         3,
                4,         5,         6,        7,
                8,         9,
                'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13,
                'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17,
                'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21,
                'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25,
                'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
                'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33,
                'y' => 34, 'z' => 35,
                'A' => 36, 'B' => 37, 'C' => 38, 'D' => 39,
                'E' => 40, 'F' => 41, 'G' => 42, 'H' => 43,
                'I' => 44, 'J' => 45, 'K' => 46, 'L' => 47,
                'M' => 48, 'N' => 49, 'O' => 50, 'P' => 51,
                'Q' => 52, 'R' => 53, 'S' => 54, 'T' => 55,
                'U' => 56, 'V' => 57, 'W' => 58, 'X' => 59,
                'Y' => 60, 'Z' => 61,
                '_' => 62, '-' => 63, '~' => 64);

//
function shortenTweetID($tweetID)
 {
    global $enring;
    $ret = "";

    for($i = $tweetID; $tweetID > 0;)
     {
        for($x = 64; $x > 1; --$x)
         {
            if(($tweetID%$x) === 0)
             {
                $ret     = $enring[$x].$ret;
                $tweetID = ($tweetID/$x);
                break;
             }
         }
        if($x === 1)
         {
            break;
         }
     }
    return $ret;
 }

function enlargeTweetID($tweetID)
 {
    global $dering;
    $ret = 1;
    $arr = preg_split('//', $tweetID, -1, PREG_SPLIT_NO_EMPTY);

    foreach($arr as $char)
     {
        $ret = ($ret * $dering[$char]);
     }

    return $ret;   
 } 
$a = 1280;
$b = shortenTweetID($a);
$c = enlargeTweetID($b);

echo $a.' : '.$b.' : '.$c;