将 base_convert 扩展到 36 以上的进制,直至 62 进制。
<?php
function base_convert_x( $_number='', $_frBase=10, $_toBase=62 ) {
$_10to62 = array(
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '00' => '0', '01' => '1', '02' => '2', '03' => '3', '04' => '4', '05' => '5', '06' => '6', '07' => '7',
'10' => 'A', '11' => 'B', '12' => 'C', '13' => 'D', '14' => 'E', '15' => 'F', '16' => 'G', '17' => 'H', '18' => 'I', '19' => 'J', '20' => 'K', '21' => 'L', '22' => 'M', '23' => 'N', '24' => 'O', '25' => 'P', '26' => 'Q', '27' => 'R',
'30' => 'U', '31' => 'V', '32' => 'W', '33' => 'X', '34' => 'Y', '35' => 'Z', '36' => 'a', '37' => 'b', '38' => 'c', '39' => 'd', '40' => 'e', '41' => 'f', '42' => 'g', '43' => 'h', '44' => 'i', '45' => 'j', '46' => 'k', '47' => 'l',
'50' => 'o', '51' => 'p', '52' => 'q', '53' => 'r', '54' => 's', '55' => 't', '56' => 'u', '57' => 'v', '58' => 'w', '59' => 'x', '60' => 'y', '61' => 'z' );
$_62to10 = array(
'0' => '00', '1' => '01', '2' => '02', '3' => '03', '4' => '04', '5' => '05', '6' => '06', '7' => '07', '8' => '08', '9' => '09', 'A' => '10', 'B' => '11', 'C' => '12', 'D' => '13', 'E' => '14', 'F' => '15', 'G' => '16', 'H' => '17',
'K' => '20', 'L' => '21', 'M' => '22', 'N' => '23', 'O' => '24', 'P' => '25', 'Q' => '26', 'R' => '27', 'S' => '28', 'T' => '29', 'U' => '30', 'V' => '31', 'W' => '32', 'X' => '33', 'Y' => '34', 'Z' => '35', 'a' => '36', 'b' => '37',
'e' => '40', 'f' => '41', 'g' => '42', 'h' => '43', 'i' => '44', 'j' => '45', 'k' => '46', 'l' => '47', 'm' => '48', 'n' => '49', 'o' => '50', 'p' => '51', 'q' => '52', 'r' => '53', 's' => '54', 't' => '55', 'u' => '56', 'v' => '57',
'y' => '60', 'z' => '61' );
$_in_b10 = 0;
$_pwr_of_frB = 1; $_chars = str_split( $_number ); $_str_len = strlen( $_number );
$_pos = 0;
while ( $_pos++ < $_str_len ) {
$_char = $_chars[$_str_len - $_pos];
$_in_b10 += (((int) $_62to10[$_char] ) * $_pwr_of_frB);
$_pwr_of_frB *= $_frBase;
}
$_dividend = (int) $_in_b10; $_in_toB = ''; while ( $_dividend > 0 ) {
$_quotient = (int) ( $_dividend / $_toBase ); $_remainder = '' . ( $_dividend % $_toBase ); $_in_toB = $_10to62[$_remainder] . $_in_toB; $_dividend = $_quotient; }
if ( $_in_toB == '' )
$_in_toB = '0';
return $_in_toB; }
echo base_convert_x( '3456789ABCDEF', 16, 10 ) . "\n"; echo base_convert( '3456789ABCDEF', 16, 10 ) . "\n"; ?>