Return to Snippet

Revision: 18641
at October 5, 2009 14:33 by ajbatac


Initial Code
<?php
// ---------------------------------------------------------------
// Converts decimal to alphanumeric values
// ---------------------------------------------------------------
// 62 possibles per letter, counting from 1.
// With 1 letter,  61 URLS
// With 2 letters, 3,843 URLS
// With 3 letters, 238,327 URLS
// With 4 letters, 15,252,991 URLS
// ---------------------------------------------------------------

$chrs = 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');

function int_to_alph($int, $chrs)
{
  $base = sizeof($chrs);
  do {
    $alph = $chrs[($int % $base)] . $alph;
  } while($int = intval($int / $base));
  return $alph;
}

function alph_to_int($alph, $chrs)
{
  $base = sizeof($chrs);
  for($i = 0, $int = 0; $i < strlen($alph); $i++)
  {
    $int += intval(array_search(substr($alph, strlen($alph) - $i - 1, 1), $chrs)) * pow($base, $i);
  }
  return $int;
}
?>

Initial URL


Initial Description


Initial Title
Short URL function

Initial Tags
url

Initial Language
PHP