How to increment an alphanumeric string in PHP?

The @simshaun dont work for me. I check the documentation and I found base_convert that can work for you (on base 35) and a comment of "francesco[at]paladinux.net" with a function to work on base65.

so the solution can be:

convert to b10 -> increment +1 -> convert base 65

EDIT 1

talking about convert I think to base64 coding so I wrote this 2 funcs using base64 encode/decode numbers. Unfortunately the charset used is a bit larger: [a-zA-Z0-9/+=], but using an internal function more efficent.

The Zero 0 is "AA=="

function nencode($in){
        $res ="";
        while(1){
                $b = $in & 0xff;
                $res = chr($b) . $res;

                if($in > 0xff){
                        $in = $in >> 8;
                } else {
                        break;
                }
        }
        return base64_encode($res);
}

function ndecode($in){

        $rv=0;
        $res =base64_decode($in);
        for($t = 0 ; $t < strlen($res) ; $t++){
                if($t>0){
                        $rv = $rv << 8;
                }
                $c = ord($res{$t});
                $rv |= $c;
        }
        return $rv;
}

This works for me:

<?php
$str = 'a';
echo ++$str; // b

$str = 'z';
echo ++$str; // aa

$str = 'aA';
echo ++$str; // aB

Try this function:

<?php
function increment(&$string){
    $last_char=substr($string,-1);
    $rest=substr($string, 0, -1);
    switch ($last_char) {
    case '':
        $next= 'a';
        break;
    case 'z':
        $next= 'A';
        break;
    case 'Z':
        $next= '0';
        break;
    case '9':
        increment($rest);
        $next= 'a';
        break;
    default:
        $next= ++$last_char;
    }
    $string=$rest.$next;
}

    //sample
    $string='a';
    for($i=1;$i<=128;$i++){
        echo $string."<br>";
        increment($string);
    }
    ?>

I think that this fundamentally does what you're after.

<?php

$chars = 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',
    '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',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

$increment1 = 0;
$increment2 = 0;

for ($i = 0; $i <= 200; $i++) {
    if ($increment2 === 62) {
        $increment1++;
        $increment2 = 1;
    } else {
        $increment2++;
    }
    echo "{$chars[$increment1]}{$chars[$increment2]} ";
}

/*
Output:
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 
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 
0 1 2 3 4 5 6 7 8 9 aa ab ac ad ae af ag ah ai aj ak al
am an ao ap aq ar as at au av aw ax ay az aA aB aC aD aE 
aF aG aH aI aJ aK aL aM aN aO aP aQ aR aS aT aU aV aW aX 
aY aZ a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 ba bb bc bd be bf bg 
bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz 
bA bB bC bD bE bF bG bH bI bJ bK bL bM bN bO bP bQ bR bS 
bT bU bV bW bX bY bZ b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ca cb 
cc cd ce cf cg ch ci cj ck cl cm cn co
*/

Tags:

Php

String