No API calls! And I threw in converting a media_id
to a shortcode
as an added bonus.
Based on slang's amazing work for figuring out the conversion. Nathan's work converting base10 to base64 in php. And rgbflawed's work converting it back the other way (with a modified alphabet). #teameffort
function mediaid_to_shortcode($mediaid){
if(strpos($mediaid, '_') !== false){
$pieces = explode('_', $mediaid);
$mediaid = $pieces[0];
$userid = $pieces[1];
}
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$shortcode = '';
while($mediaid > 0){
$remainder = $mediaid % 64;
$mediaid = ($mediaid-$remainder) / 64;
$shortcode = $alphabet{$remainder} . $shortcode;
};
return $shortcode;
}
function shortcode_to_mediaid($shortcode){
$alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$mediaid = 0;
foreach(str_split($shortcode) as $letter) {
$mediaid = ($mediaid*64) + strpos($alphabet, $letter);
}
return $mediaid;
}