http://us3.php.net/manual/en/function.base-convert.php#52450
<?php
// Decimal > Custom
function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
}
$out = "";
// this fix partially breaks when $num=0, but fixes the $num=238328 bug
// also seems to break (adds a leading zero) at $num=226981 through $num=238327 *shrug*
// for ( $t = floor( log10( $num ) / log10( $base - 1 ) ); $t >= 0; $t-- ) {
// original code:
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
?>
参数:
$num - 你的十进制整数
$base - 您希望将$num 转换为的基数(如果您提供$index,则将其保留为0,如果您使用默认值(62)则省略)
$index - 如果您希望使用默认数字列表 (0-1a-zA-Z),请忽略此选项,否则提供字符串(例如:“zyxwvu”)
<?php
// Custom > Decimal
function any2dec( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
}
$out = 0;
$len = strlen( $num ) - 1;
for ( $t = 0; $t <= $len; $t++ ) {
$out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t );
}
return $out;
}
?>
参数:
$num - 您的自定义号码(字符串)(例如:“11011101”)
$base - 编码$num 的基数(如果您提供$index,则将其保留为0,如果您使用默认值(62)则省略)
$index - 如果您希望使用默认数字列表 (0-1a-zA-Z),请忽略此选项,否则提供字符串(例如:“abcdef”)