<?php
/**
* php 根据自增id创建唯一编号类
* Date: 2016-11-27
* Author: fdipzone
* Ver: 1.0
*
* Func
* Public create 创建编号
*/
class IDCode{ // class start
/**
* 创建编号
* @param Int $id 自增id
* @param Int $num_length 数字最大位数
* @param String $prefix 前缀
* @return String
*/
public static function create($id, $num_length, $prefix){
// 基数
$base = pow(10, $num_length);
// 生成字母部分
$division = (int)($id/$base);
$word = '';
while($division){
$tmp = fmod($division, 26); // 只使用26个大写字母
$tmp = chr($tmp + 65); // 转为字母
$word .= $tmp;
$division = floor($division/26);
}
if($word==''){
$word = chr(65);
}
// 生成数字部分
$mod = $id % $base;
$digital = str_pad($mod, $num_length, 0, STR_PAD_LEFT);
$code = sprintf('%s-%s-%s', $prefix, $word, $digital);
return $code;
}
} // class end
?>
<?php
require 'IDCode.class.php';
$test_ids = array(1,9,10,99,100,999,1000,1009,2099,3999,9999,14999,99999);
foreach($test_ids as $test_id){
echo $test_id.' = '.IDCode::create($test_id, 3, 'F').'<br>';
}
?>
1 = F-A-001 9 = F-A-009 10 = F-A-010 99 = F-A-099 100 = F-A-100 999 = F-A-999 1000 = F-B-000 1009 = F-B-009 2099 = F-C-099 3999 = F-D-999 9999 = F-J-999 14999 = F-O-999 99999 = F-VD-999
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有