2017-03-17 50 views
1

我想知道如何只用小写字母(a-z)和数字(0-9)来增加一个字母数字字符串。我试图检查所有可能的字符串组合,长度为64个字符,所以字符串看起来像 g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gh (对于那些你认为它是字符串散列函数的种子)。该字符串需要从末尾到末尾递增。有没有办法增加它?如何在PHP中使用小写字母和数字来增加字母数字字符串?

+0

你的意思是增加一个,用“i”变换最后的“h”? –

+1

这是更复杂的解决 –

+0

这些问题之一请提供一些相关的代码示例。我们很乐意为您提供帮助,但我们现在无法看到进一步的信息或屏幕.... –

回答

1

我们定义了一个字母表“abcdefghijklmnopqstuvxzyz”。假设增量会从工作结束到开始和每个增量将“增加”一个字母的ASCII值:

例如:

“一”将成为“B”

“0” 将变成 “1”

“9” 将变成 “一个”

“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 = ""; 
for ($i=97;$i<=122;$i++) { 
    $alphabet .= chr($i); 
} 
$increment = new Increment($alphabet); 
$next = $increment->getNext($text); 

print_r($next.PHP_EOL); // outputs g67de5c1e86bc123442db60ae9ce615042dbf4e14e7z481ba3c1c9c3219101gi 
+0

非常感谢!我甚至从来没有想过添加我自己的字母表并使用它! –

+0

我很高兴我能帮忙,如果你认为这是正确的答案,你应该标记它 –

+0

我刚刚创建了我的计算器帐户,所以我的标记不会出现,因为我还没有15的声望。再次感谢! –

相关问题