自动增加字母数字php,javascript

时间:2015-05-10 13:53:50

标签: php jquery auto-increment alphanumeric

我想制作一个自动增量字母数字,这仅用于输出

  

如果按下按钮,则输出AA001,然后再次按下按钮,输出AA002

3 个答案:

答案 0 :(得分:1)

这并没有使用任何聪明的'技巧,但更短,更容易理解和调整。它也使用数字,因此它不会快速增长字符串的长度。它从0-9增加到a-z,但是你应该输入' a'首先。输出始终以字母字符开头,只要您的输入也以字母字符开头,因此它可以用作PHP变量名称

var nextVarName = function(str) {
  // Position of char to change.
  var change = str.length - 1;
  // The value of that char.
  var change_char = str[change];
  // Number of zeros to append when flipping.
  var zeros = 0;
  // Iterate backwards while there's a z (flipping).
  while (change_char == 'z') {
    // Increase the length of appended zeros
    zeros++;
    // Move the char to change back.
    change_char = str[--change];
  }
  if (change_char == undefined) {
    // Full flip - string increases in length.
    str = 'a' + Array(str.length + 1).join("0");
  } else {
    // Normal increment with partial flip and 9->a handling.
    str = str.substr(0, change) 
      + (change_char == '9' ? 'a' : String.fromCharCode(str.charCodeAt(change) + 1)) 
      + Array(zeros + 1).join('0');
  }
  return str;
};

var vname = 'a';
for (var i = 0; i < 5; i++) {
  vname = nextVarName(vname);
  console.log(vname);
}

结果如下:

z ---&gt; A0

9z ---&gt; a0(意外输入)

ab1zde ---&gt; ab1zdf

abcdzz ---&gt; abce00

zzzzz ---&gt; A00000

abcyzz ---&gt; abcz00

9000 ---&gt; 9001(意外输入)

https://jsfiddle.net/e20kfvky/

为每个长度创建了多少变量(以字母char开头)的计划如下:

1:26,2:936,3:33696,4:1213056 ... n:36 ^ n - 10 * 36 ^ (n - 1)

答案 1 :(得分:0)

以下代码将生成一个可以前进的令牌。它使用一些小类来使其模块化。您附加到按钮的功能将调用令牌旁边的功能,如最下方所示。

&#13;
&#13;
//Element in a big token
function Increment(startval, endval) {
  this.start = startval.charCodeAt(0);
  this.cur = this.start;
  this.end = endval.charCodeAt(0);

  //Returns the current value
  this.get = function() {
    if (this.cur <= this.end) {
      return String.fromCharCode(this.cur);
    } else {
      return null;
    }
  }

  //Advances the value we will show
  this.next = function() {
    this.cur += 1;
    return this.get();
  }

  //Reset it to the beginning
  this.reset = function() {
    this.cur = this.start;
  }
}

function Token() {
  this.token = [
    new Increment("A", "Z"),
    new Increment("A", "Z"),
    new Increment("0", "9"),
    new Increment("0", "9"),
    new Increment("0", "9")
  ];
  this.get = function() {
    return this.token.map(function(cur) {
      return cur.get();
    }).join("");
  }
  this.next = function() {
    //From the back to the front
    for (var i = this.token.length - 1; i >= 0; i--) {
      if (this.token[i].next() == null) {
        //If we exhausted all possible values, continue
        this.token[i].reset();
      } else {
        //Until we advance one that has still values left
        break;
      }
    }
    return this.get();
  }
}

//Now we are just showing off...
var a = new Token();
for (var i = 0; i < 15; i++) {
  console.log(a.next());
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

DateField

结果如下:
ab1zde ---&gt; AB1ZDF
abcdzz ---&gt; ABCEAA
zzzzz ---&gt; AAAAAA
abcyzz ---&gt; ABCZAA
9000 ---&gt; 9001

相关问题