您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
完全参照Java 改写了一版本PHP,希望能提供方便。
<?php Class Base62 { private static $encodes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* public static StringBuffer encodeBase62(byte[] data) { StringBuffer sb = new StringBuffer(data.length * 2); int pos = 0, val = 0; for (int i = 0; i < data.length; i++) { val = (val << 8) | (data[i] & 0xFF); pos += 8; while (pos > 5) { char c = encodes[val >> (pos -= 6)]; sb.append( c == 'i' ? "ia" : (c == '+' ? "ib" :( c == '/' ? "ic" : c))); val &= ((1 << pos) - 1); } } if (pos > 0) { char c = encodes[val << (6 - pos)]; sb.append( c == 'i' ? "ia" : (c == '+' ? "ib" :( c == '/' ? "ic" : c))); } return sb; } */ public static function base62_encode($data) { $sb = ''; $len = strlen($data); $pos = $val = 0; for($i= 0; $i < $len; $i++){ $tv = substr($data, $i, 1); $val = ($val << 8) | (ord($tv) & 0xFF); $pos += 8; while($pos > 5){ $c = substr(self::$encodes, $val >> ($pos -= 6), 1); $sb .= $c == 'i' ? "ia" : (($c == '+') ? "ib" : (($c == '/') ? 'ic' : $c)); $val &= ((1<<$pos) -1); } } if($pos > 0){ $c = substr(self::$encodes, $val << (6 - $pos), 1); $sb .= ($c == 'i') ? "ia" : (($c == '+') ? "ib" : (($c == '/') ? 'ic' : $c)); } return $sb; } /* public static byte[] decodeBase62(char[] data) { ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length); int pos = 0, val = 0; for (int i = 0; i < data.length; i++) { char c = data[i]; if (c == 'i') { c = data[++i]; c = c == 'a' ? 'i' : (c == 'b' ? '+' : (c == 'c' ? '/' : data[--i])); } val = (val << 6) | decodes[c]; pos += 6; while (pos > 7) { baos.write(val >> (pos -= 8)); val &= ((1 << pos) - 1); } } return baos.toByteArray(); } */ public static function base62_decode($data) { $baos = ''; $pos = $val = 0; $len = strlen($data); for($i = 0; $i < $len; $i++){ $c = substr($data, $i, 1); if($c == 'i'){ $c = substr($data, ++$i, 1); $c = ($c == 'a') ? 'i' : ($c == 'b' ? '+' : ($c == 'c' ? '/' : substr($data, --$i, 1))); } $val = ($val << 6) | strpos(self::$encodes, $c); $pos += 6; while($pos > 7){ $tv = ($val >> ($pos -= 8)); //var_dump($tv); $baos .= chr($tv); $val &= ((1 << $pos) - 1); } } return $baos; } } foreach([1,2,3,'a','b', 'c', 'addddd', '111', 'fkdskfjdsljflsdjfljsaldkjflaskjfldskjlfkjdslkfjladskjfldsk'] as $v){ var_dump($v); $b = Base62::base62_encode($v); var_dump($b); var_dump(base64_encode($v)); var_dump(Base62::base62_decode($b)); echo "----------------------\n"; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。