3v4l.org

run code in 300+ PHP versions simultaneously
<?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('yuxZe7FtJ'));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught Error: Non-static method Base62Parse::decode() cannot be called statically in /in/chmi4:119 Stack trace: #0 {main} thrown in /in/chmi4 on line 119
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Deprecated: Non-static method Base62Parse::decode() should not be called statically in /in/chmi4 on line 119 Deprecated: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Deprecated: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Deprecated: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 59 int(3472804881827743)
Output for 7.3.32 - 7.3.33, 7.4.26
int(3472804881827743)
Output for 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Strict Standards: Non-static method Base62Parse::decode() should not be called statically in /in/chmi4 on line 119 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 59 int(3472804881827743)
Output for 5.0.0 - 5.0.4
Strict Standards: Non-static method Base62Parse::decode() should not be called statically in /in/chmi4 on line 119 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 56 Strict Standards: Non-static method Base62Parse::decodeSegment() should not be called statically in /in/chmi4 on line 59 Notice: Use of undefined constant PHP_INT_SIZE - assumed 'PHP_INT_SIZE' in /in/chmi4 on line 62 Warning: shell_exec(): Unable to execute 'expr 3472804881827743 / 4294967296' in /in/chmi4 on line 64 string(16) "3472804881827743"
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/chmi4 on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/chmi4 on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/chmi4 on line 13
Process exited with code 255.

preferences:
237.26 ms | 401 KiB | 353 Q