如何在PHP中仅使用小写字母和数字来增加字母数字字符串?
我想知道如何用小写字母(a-z)和数字(0-9)来增加字母数字字符串。我试图检查字符串的所有可能组合,长度为64个字符,因此字符串看起来像如何在PHP中仅使用小写字母和数字来增加字母数字字符串?,php,Php,我想知道如何用小写字母(a-z)和数字(0-9)来增加字母数字字符串。我试图检查字符串的所有可能组合,长度为64个字符,因此字符串看起来像 g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gh (对于有这种想法的人来说,字符串是散列函数的种子)。字符串需要从头到尾递增。有办法增加它吗?我们定义了一个字母表“0123456789abcdefghijklmnopqstuvxzyz”。假设一个增量从头到尾都有效,并且每个增量将
g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gh
(对于有这种想法的人来说,字符串是散列函数的种子)。字符串需要从头到尾递增。有办法增加它吗?我们定义了一个字母表“0123456789abcdefghijklmnopqstuvxzyz”。假设一个增量从头到尾都有效,并且每个增量将向字母表的ASCII值“添加”一个:
例如:
“a”将变成“b”
“0”将变为“1”
“9”将成为“a”
“z”将变为“0”
“abc”->“abd”
“01z”->“020”
等
以下算法将起作用:
<?php
class Increment {
private $alphabet;
public function __construct($alphabet)
{
$this->alphabet = $alphabet;
}
public function getNext($text)
{
$length = strlen($text);
$increment = true;
for ($i=$length; $i--; $i > 0) {
$currentCharacter = $text[$i];
if ($increment) {
$increment = $this->hasReachEndOfAlphabet($currentCharacter);
$text[$i] = $this->getIncrementedCharacter($currentCharacter);
}
}
return $text;
}
private function getIncrementedCharacter($currentCharacter)
{
$position = strpos($this->alphabet, $currentCharacter);
if (!$this->hasReachEndOfAlphabet($currentCharacter)) {
return $this->alphabet[++$position];
}
return $this->alphabet[0];
}
private function hasReachEndOfAlphabet($currentCharacter)
{
$position = strpos($this->alphabet, $currentCharacter);
if ($position < strlen($this->alphabet) -1) {
return false;
}
return true;
}
} //end of class
$text = "g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gh";
$alphabet = "0123456789";
for ($i=97;$i<=122;$i++) {
$alphabet .= chr($i);
}
$increment = new Increment($alphabet);
$next = $increment->getNext($text);
print_r($next.PHP_EOL); // outputs g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gi
增量为1是指将最后一个“h”转换为“i”?这是一个更复杂的问题,要理解它,请提供一些相关的代码示例。我们很乐意帮助您,但我们现在看不到更远处或您的屏幕……,或者至少看不到足够近的距离-将一个较大的基数36转换为十进制,递增,然后再转换回来。非常感谢!我从来没有想过要添加我自己的字母表并使用它!