在PHP中用z后的数字增加字母数字

时间:2013-12-15 03:57:18

标签: php increment alphanumeric

我希望在php中增加字符串,在z之后,下一个增量是数字。

a..aa..ab..az..a0

我尝试过以下几次尝试,非常感谢任何想法或建议。

<?php

$start = 'a';
$end = '999';
while($start != $end)
{
    $start = ++$start;
    echo $start . "\n";
}

然而结果看起来像这样

a
b
...
aaaa

如果我在$ start的末尾添加一个数字,如下所示

<?php

$start = 'a1';
$end = '999';
while($start != $end)
{
    $start = ++$start;
    echo $start . "\n";
}

结果看起来像这样

a1
a2
...
aa1
...
az9
ba0

我要找的结果应该是

a1
a2
...
aa1
...
az9
a00
a01
...
ba0

3 个答案:

答案 0 :(得分:1)

嗯,++在它的作用中非常严格地定义 - 对于非常有限的字符范围,按ASCII值递增字符,因此您的问题很可能需要自定义解决方案。

我试过了:

$start = 'a';
$end = '999';
$seq = 'abcdefghijklmnopqrstuvwxyz0123456789';

while($start != $end)
{
    echo $start . "\n";

    $pass = 1;
    for ($i = strlen($start) - 1; $i >= 0; $i--) {
        if (!$pass) break;
        else $pass--;

        $last = substr($start, $i, 1);
        $pos = strpos($seq, $last);

        if ($pos == strlen($seq) - 1) {
            $pos = 0;
            $pass++;
        } else $pos++;
        $start[$i] = $seq[$pos]; 
    }
    if ($pass) $start .= substr($seq, 0, 1);
}

如果您接受我的评论,那么您可以获得您想要的序列。所有代码都是向后手动经过$start,将字符递增到$seq中的下一个字符,如果是最后一个,则将其设置为第一个并“携带一个”。真的,这只是一个角色总和。

答案 1 :(得分:1)

我建议改用range()for循环。以下是我理解的问题解决方案。但是再次查看你的问题&amp;你想要的输出,还不清楚这是否适合你。

// First, generate an array consisting of the alhabet.
$alphabet = range('a','z');

// Now, set a min & a max number to increment.
$number_min = 0;
$number_max = 999;

// Now loop through the numbers via 'for' loop.
for ($count = $number_min; $count <= $number_max; $count++) {

  // And loop through the alphabet via a 'foreach' loop.
  foreach ($alphabet as $letter_key => $letter_value) {

    // Set an append value if the '$count' value is greater than the '$number_min' value.
    $append_value = (($count > $number_min) ? $count : null);

    // Now echo the '$letter_value' and the '$append_value.'
    echo $letter_value . $append_value . '<br />';

  } // foreach

} // for

编辑以下是使用modulus operator进行此问题的另一个问题。似乎没有100%喜欢你想要的,但相信这更接近?

// First, generate an array consisting of the alhabet.
$alphabet = range('a','z');

// Now, set a min & a max number to increment.
$number_min = 0;
$number_max = 999;

// Now loop through the numbers via 'for' loop.
for ($count = $number_min; $count <= $number_max; $count++) {

  // And loop through the alphabet via a 'foreach' loop.
  foreach ($alphabet as $letter_key => $letter_value) {

    // Set null values for the append variables.
    $numerical_append = $alphabet_append = null;

    if ($count > $number_min) {

      // Set a numerical append value if the '$count' value is greater than the '$number_min' value.
      $numerical_append = $count;

      // Get the alphabet key based on the '$count' & loop it via a modulous of the size of the '$alphabet' array.
      $alphabet_key = ($count % (count($alphabet)+1)) - 1;

      // Set an alphabetical append value if the '$count' value is greater than the '$number_min' value.
      $alphabet_append = $alphabet[$alphabet_key];
    }

    // Now echo the '$letter_value' and the '$numerical_append.'
    echo $letter_value . $alphabet_append . $numerical_append . '<br />';

  } // foreach

} // for

答案 2 :(得分:1)

为什么不使用base_convert

从基数10传递到36(最大为base_convert),你将得到你想要的东西。

序列将是:0123456789abcdefghijklmnopqrstuvwxyz

这与#Naltharial的答案不一样,但在我看来,这就是你想要的。

例如:

    $var = base_convert('az9',36,10);
    echo base_convert($var,10,36).PHP_EOL; $var+=1;
    echo base_convert($var,10,36).PHP_EOL; $var+=1;
    echo base_convert($var,10,36).PHP_EOL.PHP_EOL; $var+=1;

    $var = base_convert('azz',36,10);
    echo base_convert($var,10,36).PHP_EOL; $var+=1;
    echo base_convert($var,10,36).PHP_EOL; $var+=1;
    echo base_convert($var,10,36).PHP_EOL; $var+=1;

将打印:

    az9
    aza
    azb

    azz
    b00
    b01

您真正希望序列为:abcdefghijklmnopqrstuvwxyz0123456789

然后你可以这样做:

    // $string is the string in base36
    $length = strlen($string);
    $result = '';
    $base36  = '0123456789abcdefghijklmnopqrstuvwxyz';
    $ownBase = 'abcdefghijklmnopqrstuvwxyz0123456789';
    for ($i=0; $i<$length; $i++) {
        $result .= $ownBase[strpos($string[$i],$base36)];
    }

您的功能可以是:

    // Set a min & a max number to increment.
    $number_min = '0';
    $number_max = '999';
    // Transform in number
    $length     = strlen($number_max);
    $result     = '';
    $base36     = '0123456789abcdefghijklmnopqrstuvwxyz';
    $ownBase    = 'abcdefghijklmnopqrstuvwxyz0123456789';
    for ($i=0; $i<$length; $i++) {
        $result .= $base36[strpos($number_max[$i],$ownBase)];
    }
    $length     = strlen($number_min);
    // Number max in base 10 to reach
    $nbLoop     = base_convert($result,36,10);
    for ($i=0; $i<$length; $i++) {
        $result .= $base36[strpos($number_min[$i],$ownBase)];
    }
    // Number min in base 10 to reach
    $nbLoop    -= base_convert($result,36,10);
    // Printing every number :
    for ($i=base_convert($result,36,10); $i<$nbLoop; $i++) {
      $string = base_convert($i,10,36);
      $length = strlen($string);
      $result = '';
      for ($j=0; $j<$length; $j++) {
          $result .= $ownBase[strpos($string[$j],$base36)];
      }
      echo $result;
    }