Return to Snippet

Revision: 47668
at June 13, 2011 15:27 by prianto


Initial Code
<?php
define('CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
function getIDFromURL ($string, $base = CHARS){
	$length = strlen($base);
	$size = strlen($string) - 1;
	$string = str_split($string);
	$out = strpos($base, array_pop($string));
	foreach($string as $i => $char){
		$out += strpos($base, $char) * pow($length, $size - $i);
	}
	return $out;
}
function getURLFromID ($integer, $base = CHARS){
	$length = strlen($base);
	while($integer > $length - 1){
		$out = $base[fmod($integer, $length)] . $out;
		$integer = floor( $integer / $length );
	}
	return $base[$integer] . $out;
}
?>

Initial URL
http://prianto.net

Initial Description


Initial Title
PHP URL Shortener

Initial Tags
url, php

Initial Language
PHP