五、十进制转64进制算法
1、算法1
复制代码 代码示例:
const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
/**
* 将64进制的数字字符串转为10进制的数字字符串
* @param $m string 64进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
*/
function hex64to10($m, $len = 0) {
$m = (string)$m;
$hex2 = '';
$Code = KeyCode;
for($i = 0, $l = strlen($Code); $i < $l; $i++) {
$KeyCode[] = $Code[$i];
}
$KeyCode = array_flip($KeyCode);
for($i = 0, $l = strlen($m); $i < $l; $i++) {
$one = $m[$i];
$hex2 .= str_pad(decbin($KeyCode[$one]), 6, '0', STR_PAD_LEFT);
}
$return = bindec($hex2);
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将10进制的数字字符串转为64进制的数字字符串
* @param $m string 10进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
*/
function hex10to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = decbin($m);
$hex2 = str_rsplit($hex2, 6);
$hex64 = array();
foreach($hex2 as $one) {
$t = bindec($one);
$hex64[] = $KeyCode[$t];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将16进制的数字字符串转为64进制的数字字符串
* @param $m string 16进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
*/
function hex16to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = array();
for($i = 0, $j = strlen($m); $i < $j; ++$i) {
$hex2[] = str_pad(base_convert($m[$i], 16, 2), 4, '0', STR_PAD_LEFT);
}
$hex2 = implode('', $hex2);
$hex2 = str_rsplit($hex2, 6);
foreach($hex2 as $one) {
$hex64[] = $KeyCode[bindec($one)];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 功能和PHP原生函数str_split接近,只是从尾部开始计数切割
* @param $str string 需要切割的字符串
* @param $len integer 每段字符串的长度
* @return array
*/
function str_rsplit($str, $len = 1) {
if($str == null || $str == false || $str == '') return false;
$strlen = strlen($str);
if($strlen <= $len) return array($str);
$headlen = $strlen % $len;
if($headlen == 0) {
return str_split($str, $len);
}
$return = array(substr($str, 0, $headlen));
return array_merge($return, str_split(substr($str, $headlen), $len));
}
$a=idate("U");
echo "rn<br />e:" . hex10to64($a);
echo "rn<br />e:" . hex64to10(hex10to64($a));
2、算法2
复制代码 代码示例:
function dec2s4($dec) {
$base = '0123456789_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';
do {
$result = $base[$dec % 64] . $result;
$dec = intval($dec / 64);
} while ($dec != 0);
return $result;
}
function s42dec($sixty_four) {
$base_map = array ( '0' => 0,'1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,'7' => 7,'8' => 8,'9' => 9,'_' => 10,'$' => 11,'a' => 12,'b' => 13,'c' => 14,'d' => 15,'e' => 16,'f' => 17,'g' => 18,'h' => 19,'i' => 20,'j' => 21,'k' => 22,'l' => 23,'m' => 24,'n' => 25,'o' => 26,'p' => 27,'q' => 28,'r' => 29,'s' => 30,'t' => 31,'u' => 32,'v' => 33,'w' => 34,'x' => 35,'y' => 36,'z' => 37,'A' => 38,'B' => 39,'C' => 40,'D' => 41,'E' => 42,'F' => 43,'G' => 44,'H' => 45,'I' => 46,'J' => 47,'K' => 48,'L' => 49,'M' => 50,'N' => 51,'O' => 52,'P' => 53,'Q' => 54,'R' => 55,'S' => 56,'T' => 57,'U' => 58,'V' => 59,'W' => 60,'X' => 61,'Y' => 62,'Z' => 63); $result = 0;
$len = strlen($sixty_four);
for ($n = 0; $n < $len; $n++) {
$result *= 64;
$result += $base_map[$sixty_four{$n}];
}
return $result;
}
$a=idate("U");
var_dump(dec2s4($a));
var_dump(s42dec(dec2s4($a)));
3、算法效率测试
复制代码 代码示例:
$strarr = array();
$time1 = microtime(true);
for($i = 0; $i < 10000; ++$i) {
$str = idate("U")+$i;
$strarr[] = "{$i}->$strrn<br>";
}
$time2 = microtime(true);
$time3 = $time2 - $time1;
$time1 = microtime(true);
for($i = 0; $i < 10000; ++$i) {
$str = dec2s4(idate("U")+$i);
$strarr[] = "{$i}->$strrn<br>";
}
$time2 = microtime(true);
echo "rn<br />运行10000次用时(秒):" . ($time2 - $time1 - $time3);
4、测试结果
算法1:0.1687250137329
算法2:0.044965028762817
5、结论
算法1虽然效率上差一些,但是可以把md5生成的16进制转化为64进制,能够使用在必须使用md5的环境下缩短字符串。
六、总结
本文涉及了上传图片重命名可以能使用的几种方法,其中关键点是使用10进制转换为64进制来进行字符串的缩减。
例如,使用fast_uuid生成的17位数字,转换为64进制仅有7位字符;
具体使用,可以根据自身情况灵活使用,希望对大家有所帮助。