Alphanumeric Increment a string in PHP (To a certain length)

前端 未结 5 1969
鱼传尺愫
鱼传尺愫 2020-12-21 05:50

I need to generate a sequence (or function to get a \"next id\") with an alphanumeric incrementor.

The length of the string must be defineable, and the Characters mu

5条回答
  •  没有蜡笔的小新
    2020-12-21 06:11

    function formatPackageNumber($input)
     { 
      //$input = $_GET['number'];
    
      $alpha_array = array("A", "B" , "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
      $number_array = array("0", "1" , "2", "3", "4", "5", "6", "7", "8", "9");
      $output = "";
    
      for($i=0; $i<=5; $i++){
         if($i>=4) {
          $divisor = pow(26,$i-3)*pow(10,3);
        } else {
          $divisor = pow(10,$i);
        }
        $pos = floor($input/$divisor);
    
        if($i>=3) {
          $digit = $pos%26;
          $output .= $alpha_array[$digit];
        } else {
          $digit = $pos%10 ;
          $output .= $number_array[$digit];
        }
      } 
    return  strrev($output);
    

    }

提交回复
热议问题