我很确定 YouTube 只是在 base-X 系统中编码整数 ID。有这么多,而且它们创建得如此之快,以至于它们似乎是随机的。
代码看起来像:
<?php
$base_str = '0123456789abcdefghijklmnopqrstuvwxyz-_';
$base = strlen($base_str);
// generate a number if no input
if( ! isset($argv[1]) ) {
$number = rand(1000,1000000);
} else {
$number = intval($argv[1]);
}
printf("Input: %d\n", $number);
printf("Base: %d\n", $base);
// will hold the base-X encoded representation of the number
$repr = '';
for( $i=$number; $i>0; ) {
$remainder = $i % $base;
$digit_repr = substr($base_str, $remainder, 1);
$repr = $digit_repr . $repr;
printf("Rem: %2d Repr: %s Cur: %16d Progress: %s\n", $remainder, $digit_repr, $i, $repr);
$i = ($i - $remainder) / $base;
}
示例输出:
Input: 2000000
Base: 38
Rem: 22 Repr: m Cur: 2000000 Progress: m
Rem: 1 Repr: 1 Cur: 52631 Progress: 1m
Rem: 17 Repr: h Cur: 1385 Progress: h1m
Rem: 36 Repr: - Cur: 36 Progress: -h1m
如果您想在 ID 的外观中引入更多“随机性”,您可以随时加扰$base_str
。请记住,在开始编码 ID 之前,您只能对其进行一次加扰。
解码
我想这很重要,对吧?
<?php
$base_str = '0123456789abcdefghijklmnopqrstuvwxyz-_';
$base = strlen($base_str);
if( ! isset($argv[1]) ) {
$input = '-h1m';
} else {
$input = $argv[1];
}
printf("Input: %s\n", $input);
printf("Base: %d\n", $base);
$repr = str_split($input);
$number = 0;
for( $i=0; $i<count($repr); $i++) {
$number = $number * $base;
$value = strpos($base_str, $repr[$i]);
$number += $value;
printf("Char: %s Value: %2d Cur: %12d\n", $repr[$i], $value, $number);
}
示例输出:
Input: -h1m
Base: 38
Char: - Value: 36 Cur: 36
Char: h Value: 17 Cur: 1385
Char: 1 Value: 1 Cur: 52631
Char: m Value: 22 Cur: 2000000