数字类型自动编码函数

/**
 * 数字类型自动编码函数
 * @param int $index 初始索引
 * @param int $step 自动增加步长
 * @param int $digits 位数,5为00000
 * @return mixed 根据三个参数生成的自动编码
 * @author mrz <i@mrz.cc>
 */
function auto_encode( $index, $digits = 5, $step = 1 ) {
    //编码基数,5位数即等于00000
    $encode_basic = str_repeat( '0', $digits );
    //索引的位数
    $index_digits = strlen( $index );
    //如果索引位数大于指定位数,更新基数
    if ( $index_digits > $digits ) {
        $digits_offset = $index_digits - $digits;
        $encode_basic = str_repeat( '0', (int) ($digits + $digits_offset) );
    }
    $new_encode = (int) ($index + $step);
    $new_encode_digits = strlen( $new_encode ); //自动编码后的位数,用来索引替换基数里的位置
    $auto_encode = substr_replace( $encode_basic, $new_encode, -($new_encode_digits), $new_encode_digits );
    return $auto_encode;
}
原文地址:https://www.cnblogs.com/banyanCheung/p/3006914.html