php 在数字中插入字符,如何在PHP中使用小写字母和数字增加字母数字字符串?...

我们定义了一个字母“0123456789abcdefghijklmnopqstuvxzyz”。假设增量从末尾到开头起作用,每个增量将“加”一个字母表的ASCII值:

例如:

"a" will become "b"

"0" will become "1"

"9" will become "a"

"z" will become "0"

"abc" -> "abd"

"01z" -> "020"

..

etc

以下算法将起作用:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值