@ 2012-09-10T10:36:48Z <?php
/**
* BASE62 解析类,专门针对长mid与62进制互转,不适用其他操作
*
* @copyright (c) 2009, 新浪网MiniBlog All rights reserved.
* @author 王兆源 <zhaoyuan@staff.sina.com.cn> 朱建鑫 李如其
* @version 1.0 - 2009-08-05
* @package Tools
*/
class Base62Parse
{
private static $string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
private static $encodeBlockSize = 7;
private static $decodeBlockSize = 4;
/**
* 将mid转换成62进制字符串
*
* @param string $mid
* @return string
*/
function encode($mid)
{
$str = "";
$midlen = strlen($mid);
$segments = ceil($midlen / self::$encodeBlockSize);
$start = $midlen;
for ( $i = 1; $i < $segments; $i+=1 )
{
$start -= self::$encodeBlockSize;
$seg = substr($mid, $start, self::$encodeBlockSize);
$seg = self::encodeSegment($seg);
$str = str_pad($seg, self::$decodeBlockSize, '0', STR_PAD_LEFT) . $str;
}
$str = self::encodeSegment(substr($mid, 0, $start)) . $str;
return $str;
}
/**
* 将62进制字符串转成10进制mid
*
* @param string $str
* @return string
*/
function decode($str, $compat = false, $for_mid = true)
{
$mid = "";
$strlen = strlen($str);
$segments = ceil($strlen / self::$decodeBlockSize);
$start = $strlen;
for ( $i = 1; $i < $segments; $i+=1 )
{
$start -= self::$decodeBlockSize;
$seg = substr($str, $start, self::$decodeBlockSize);
$seg = self::decodeSegment($seg);
$mid = str_pad($seg, self::$encodeBlockSize, '0', STR_PAD_LEFT) . $mid;
}
$mid = self::decodeSegment(substr($str, 0, $start)) . $mid;
//判断v3版本mid
if ( $for_mid && $mid > 4294967295 ) {
if ( PHP_INT_SIZE < 8 ) {
$cmd = "expr $mid / 4294967296";
$time = `$cmd`;
} else {
$mid += 0;
$time = $mid >> 32; //64位中前32位是时间,精确到秒级
}
if ( $time > 1230739200 && $time < 1577808000 ) return $mid;
}
//end
if ( $compat && !in_array(substr($mid, 0, 3), array('109', '110', '201', '211', '221', '231', '241')) ) {
$mid = self::decodeSegment(substr($str, 0, 4)) . self::decodeSegment(substr($str, 4));
}
if ( $for_mid ) {
if ( substr($mid, 0, 1) == '1' && substr($mid, 7, 1) == '0' ) {
$mid = substr($mid, 0, 7) . substr($mid, 8);
}
}
return $mid;
}
/**
* 将10进制转换成62进制
*
* @param string $str 10进制字符串
* @return string
*/
function encodeSegment($str)
{
$out = '';
while ( $str > 0 ) {
$idx = $str % 62;
$out = substr(self::$string, $idx, 1) . $out;
$str = floor($str / 62);
}
return $out;
}
/**
* 将62进制转换成10进制
*
* @param string $str 62进制字符串
* @return string
*/
function decodeSegment($str)
{
$out = 0;
$base = 1;
for ( $t = strlen($str) - 1; $t >= 0; $t-=1 )
{
$out = $out + $base * strpos(self::$string, substr($str, $t, 1));
$base *= 62;
}
return $out . "";
}
}
var_dump(Base62Parse::decode('ytAxFdLOl'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Fatal error: Uncaught Error: Non-static method Base62Parse::decode() cannot be called statically in /in/nVkpe:119
Stack trace:
#0 {main}
thrown in /in/nVkpe on line 119
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
43.13 ms | 401 KiB | 8 Q