class BaseConv {
private static $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
public static function Base10($value, $b = 62) {
if (!preg_match("/^[a-zA-Z0-9]+$/", $value)) return -1;
$limit = strlen($value);
$result = strpos(static::$base, $value[0]);
for($i = 1; $i < $limit; $i++) {
$result = $b * $result + strpos(static::$base, $value[$i]);
}
return $result;
}
public static function BaseX($value, $b = 62) {
$r = $value % $b;
$result = static::$base[$r];
$q = floor($value / $b);
while ($q) {
$r = $q % $b;
$q = floor($q / $b);
$result = static::$base[$r].$result;
}
return $result;
}
}
function idToUrl($id) {
return BaseConv::BaseX(100000000000 + $id);
}
function idFromUrl($id) {
return BaseConv::Base10($id)-100000000000;
}Short URLs (MySQL + PHP)
The internet has many sites that use short links, for example youtube, tinyurl and similar services. A link like http://www.youtube.com/watch?v=N98cpA5hdI0 is far nicer than http://www.youtube.com/watch?v=23450928230945820532. Short links are also necessary if we do not want our site to be easily scraped. Beginner programmers often try to create links of this kind on their own sites, but in a very strange way.