如何在PHP中编写函数来生成字符串
A0001,A0002,..A9999,... AA0001,AA9999,... AZ0001,AZ9999,...
另外,如果它有可能在mysql中编写函数,请让我知道。
我要为数据库中的记录定义唯一ID,
所以我需要生成唯一的ID。
现在我在桌上使用自动增量。现在它触摸8位数字,所以我需要将它最小化为4到6位数字。我的一个朋友说使用这个公式,所以你会尽量减少你的唯一ID的长度。
A9999 = 9,999 Records
AA0001 to AZ9999 = 259,974 Records
AA0001 to ZZ9999 = 175,742,424 Records或者如果你有任何其他想法,请给我。
提前致谢。
我的问题的GOT解决方案
感谢您的建议和想法,@thecoshman帮助我几乎解决了我的问题。
function encodeItemBarcode($number) {
$number = base_convert($number, 10, 36);
$number = strtoupper($number);
return $number;
}
function decodeItemBarcode($number) {
$number = base_convert($number, 36, 10);
$number = strtoupper($number);
return $number;
}输出:
999999999991 Encrypted as : CRE66I9J - Decrypted as : 999999999991
999999999992 Encrypted as : CRE66I9K - Decrypted as : 999999999992
999999999993 Encrypted as : CRE66I9L - Decrypted as : 999999999993
999999999994 Encrypted as : CRE66I9M - Decrypted as : 999999999994
999999999995 Encrypted as : CRE66I9N - Decrypted as : 999999999995
999999999996 Encrypted as : CRE66I9O - Decrypted as : 999999999996
999999999997 Encrypted as : CRE66I9P - Decrypted as : 999999999997
999999999998 Encrypted as : CRE66I9Q - Decrypted as : 999999999998
999999999999 Encrypted as : CRE66I9R - Decrypted as : 999999999999所以最后我把12位数字(999999999991)最小化为8个字符(CRE66I9J)。我不会将它存储在数据库中,我只会在前端使用它。
再次感谢All。