Created
November 28, 2011 14:28
-
-
Save kijtra/1400583 to your computer and use it in GitHub Desktop.
[PHP] 短縮URL等に使える、数字を短文字に変換する関数。引数が数字なら文字に、文字なら数字に相互変換。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function shorten($arg){ | |
if(empty($arg)){ | |
return NULL; | |
} | |
static $_out; | |
if(!empty($_out[$arg])){ | |
return $_out[$arg]; | |
} | |
$_out=array(); | |
$base='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$in=$arg; | |
$out=NULL; | |
if(is_numeric($in)){ | |
while($in>61){ | |
$out=$base{fmod($in,62)}.$out; | |
$in=floor($in/62); | |
} | |
$out=$base{$in}.$out; | |
}else{ | |
$size=strlen($in)-1; | |
$in=str_split($in); | |
$out=strpos($base,array_pop($in)); | |
foreach($in as $i=>$char){ | |
$out+=strpos($base,$char)*pow(62,$size-$i); | |
} | |
} | |
return $_out[$arg]=$out; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment