【问题标题】:Increment alphanumerically with numbers after z in PHP在 PHP 中 z 之后的数字按字母数字递增
【发布时间】:2014-01-02 16:04:16
【问题描述】:

我希望在 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

【问题讨论】:

  • 不应该是az9a0a,而不是a00,因为a是序列的第一个字符?

标签: php increment alphanumeric


【解决方案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 中的下一个字符,如果是最后一个字符,则将其设置为第一个字符并“携带一个”。真的,这只是一个字符和。

【讨论】:

    【解决方案2】:

    我建议改用range()for 循环。据我了解,这是我对问题的解决方案。但是再次查看您的问题和您想要的输出,仍然不清楚这是否适合您。

    // 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
    

    【讨论】:

      【解决方案3】:

      为什么不使用base_convert

      从 base 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;
          }
      

      【讨论】:

      • 令人惊讶的是,我今天再次使用此解决方案 =]
      猜你喜欢
      • 2016-09-05
      • 2014-10-08
      • 2011-07-21
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多