[php function] convert 16bits(md5) string to 64bits string on php.
You can create a short url, if you have a long url.
!!!!but I hoped more short than url.
example
long url: http://samurai-engineer.com/id/3d6e8ab5a32006ae5b2a2e8ccd8b39ff 63 lengths
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
short url: http://samurai-engineer.com/id/fmWaJqcw1GVrayWcPoIV(M 53 lengths
code
function convert_str($str, $base_number, $conv_number){
if( $base_number%2!=0 || $conv_number%2!=0 || $base_number == $conv_number ) {
return ‘can not convert’;
}
if( $base_number < $conv_number ) {
$cardinal = floor($conv_number/$base_number);
$decode = false;
}else{
$cardinal = floor($base_number/$conv_number);
$decode = true;
}
$sign = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.(‘;
$base_band = log($base_number,$cardinal);
$conv_band = log($conv_number,$cardinal);
$binary = ”;
foreach(str_split($str,$base_band) as $s) {
foreach(str_split($s) as $ss) {
$p = strpos($sign, $ss);
$bin = ”;
for($i=0;$i<log($base_number,$cardinal);$i++){
$sp = $p%$cardinal;
$p = floor($p/$cardinal);
$bin .= $sp;
}
$binary .= strrev($bin);
}
}
$r = ”;
if($decode){
$binary = preg_replace(“#{$ck}$#”, ”, $binary);
}
foreach(str_split($binary,$conv_band) as $s) {
if(strlen($s) < $conv_band){
$s = $s.str_repeat(’0′,$conv_band-strlen($s));
}
$d = 0;
$i = 0;
foreach(array_reverse(str_split($s)) as $ss) {
$d += $ss*pow($cardinal,$i);
$i++;
}
$r .= substr($sign, $d, 1);
}
return $r;
}
Use
//sample
$hex = ’3d6e8ab5a32006ae5b2a2e8ccd8b39ff’;
var_dump(convert_str($hex,16,64));
var_dump(convert_str(convert_str($hex,16,64),64,16));
parameter
$str: all length.
$base_number: use 2, 4, 8, 16, 32, 64
$conv_number: use 2, 4, 8, 16, 32, 64
notice: some time, must cut back a result string.
//sample
$hex = ’3d6e8ab5a328addafd00′;
var_dump(convert_str($hex,16,64));
var_dump(convert_str(convert_str($hex,16,64),64,16));
var_dump(substr(convert_str(convert_str($hex,16,64)64,16),0,20));
result
string(14) “fmWaJqcEHtHZ00″ string(21) “3d6e8ab5a328addafd000″ string(20) “3d6e8ab5a328addafd00″
I didn’t get an answer about this problem.
You can get answers, please comment.

