Incrementing a string by one in PHP

后端 未结 3 1391
臣服心动
臣服心动 2020-12-22 12:40

I am struggling to find a way to increment a specific pattern required. For each new member, they are given a unique ID such as ABC000001. Each new members

3条回答
  •  渐次进展
    2020-12-22 13:02

    As @axiac mentions this is probably not a good idea but it's pretty easy to manage.

    $memberid = 'ABC000001';
    list($mem_prefix,$mem_num) = sscanf($memberid,"%[A-Za-z]%[0-9]");
    echo $mem_prefix . str_pad($mem_num + 1,6,'0',STR_PAD_LEFT);
    

    Split your current member number into the alpha and numeric parts then put them back together bumping the number when you do it. I use this as a function and pass the previous ID and what I get back is the next ID in the sequence.

提交回复
热议问题