function encode_num($num = 0)
{
$num_len = strlen($num);
if (!is_numeric($num) || $num_len > 15) {
$this->data = false;
$this->message = '请输入数字';
return $this;
}
list($start_rand, $end_rand, $nums, $pies) = [rand(100, 999), rand(10, 99), [], self::unique_rand(1, 9, 9)];
for ($i = 0; $i < ($num_len / 2); $i++) {
$nums[$pies[$i]] = ['num' => substr($num, $i * 2, 2), 'rand' => rand(1000, 7000), 'pie' => $pies[$i], 'i' => $i + 1];
}
ksort($nums);
$str = $start_rand;
foreach ($nums as $maps) {
list($rand, $d_rand) = [rand(10, 70), rand(1, 9)];
$i = $rand + $maps['i'] + $d_rand;
$len=strlen($maps['num']);
$vs = ($maps['num'] * $maps['pie']) + $maps['rand'] + $start_rand - ($end_rand * $maps['i']) + $i;
$str .= $maps['rand'] . $maps['pie'] . $rand . $i . $d_rand .$len .$vs;
}
return $str . $end_rand;
}
function decode_num($num = 0)
{
if (!is_numeric($num)) {
return false;
}
if (empty($num)) $num = $this->data;
list($start_rand, $end_rand, $num_len, $encode, $nums) = [ceil(substr($num, 0, 3)), ceil(substr($num, -2)), ceil(strlen($num) - 5), substr(substr($num, 3), 0, -2), []];
for ($i = 0; $i < ceil($num_len / 15); $i++) {
$nums[] = substr($encode, $i * 15, 15);
}
$n = [];
foreach ($nums as $maps) {
list($maps_rand, $pie, $rand, $i, $d_rand, $vs,$len) = [ceil(substr($maps, 0, 4)), ceil(substr($maps, 4, 1)), substr($maps, 5, 2), intval(substr($maps, 7, 2)), substr($maps, 9, 1), ceil(substr($maps, 11)),ceil(substr($maps,10,1))];
$rule_i = intval($i - $rand - $d_rand);
$v = ceil(($vs - $i + ($end_rand * $rule_i) - $start_rand - $maps_rand) / $pie);
if($len==2 && strlen($v)==1) $v='0'.$v;
$n[$rule_i] = $v;
}
ksort($n);
return implode($n, '');
}
欢迎大家来测试体验,一起分享经验
|