How to increase alphanumeric string in PHP?

There is a string containing the characters [a-zA-Z0-9] . It should be 26 * 2 + 10 = 62 possibilities in one character and 62 ^ 2 in two. What is the preferred way to increase the value of such a string, so that "aA" becomes "aB", etc.? Is there any embedded material in PHP that can help?

I know that you can increase the string, but these are only lowercase letters. Essentially, the result should go from “a” to “aa” with 61 steps.

+8
string php
source share
6 answers

The @simshaun doesn't work for me. I check the documentation and I found base_convert that might work for you (based on 35) and the comment "francesco [at] paladinux.net" with the base65 work function.

so the solution could be:

convert to b10 → increment +1 → convert base 65

EDIT 1

Speaking of converting, I think base64 encoded, so I wrote these 2 funcs using base64 encode / decode numbers. Unfortunately, the encoding used is slightly larger: [a-zA-Z0-9 / + =], but using the internal function is more efficient.

Zero 0 - "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; } 
+2
source share

This works for me:

 <?php $str = 'a'; echo ++$str; // b $str = 'z'; echo ++$str; // aa $str = 'aA'; echo ++$str; // aB 
+4
source share

Try this feature:

 <?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); } ?> 
+2
source share

I like this. How to use: ClassX :: increment ('p04E7K', 6); ClassX :: decrement ('p04E7K', 6);

the code:

 class ClassX { public static $baseCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static function increment($x, $digits) { $charList = static::$baseCharacters; // completa cadeia de caracteres com o numero de digitos parametrizados $x = trim($x); if(strlen($x) < $digits) { $x = str_pad($x, $digits, substr($charList, 0, 1), STR_PAD_LEFT); } $result = preg_split("//", $x, -1, PREG_SPLIT_NO_EMPTY); // percorre a cadeia de caracteres de tras pra frente e incrementa o primeiro caractere possivel for ($i = $digits - 1; $i >= 0; --$i) { $char = $result[$i]; $currentChar = strpos($charList, $char); $nextCharPosition = $currentChar+1; if($nextCharPosition < strlen($charList)) { $nextChar = substr($charList, $nextCharPosition, 1); $result[$i] = $nextChar; break; } } return implode('', $result); } public static function decrement($x, $digits) { $charList = static::$baseCharacters; // completa cadeia de caracteres com o numero de digitos parametrizados if(strlen($x) < $digits) { $x = str_pad($x, $digits, substr($charList, 0, 1), STR_PAD_LEFT); } $result = preg_split("//", $x, -1, PREG_SPLIT_NO_EMPTY); // percorre a cadeia de caracteres de tras pra frente e decrementa o primeiro caractere possivel for ($i = $digits - 1; $i >= 0; --$i) { $char = $result[$i]; $currentChar = strpos($charList, $char); $previousCharPosition = $currentChar-1; if($previousCharPosition > -1) { $previousChar = substr($charList, $previousCharPosition, 1); $result[$i] = $previousChar; // define os caracteres apos o decrementado para o ultimo caractere. ex: 3[00] > 2[99] for ($j = $i + 1; $j < $digits && $i < $digits - 1; ++$j) $result[$j] = substr($charList, strlen($charList)-1, 1); break; } } return implode('', $result); } } 
+2
source share

I think this fundamentally does what you need.

 <?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: abcdefghijklmnopqrstu vwxyz ABCDEFGHIJKLMNOPQRSTU VWXYZ 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 */ 
+1
source share

I came to this question because I was also looking for a way to generate an automatic increase in the alphanumeric unique identifier, very similar to what we have in mobile money (M-PESA) in Kenya. For reference, here is a screenshot showing transactions A screenshot showing MPESA transaction examples I would like to leave it here if someone is looking for similar properties: i.e. - Auto-increment alphanumeric - All characters in uppercase - automatically increase the length of the string after it is exhausted. - an increment from 0 to 9, then from A to Z, before the increment of the adjacent character of the number and is a modification of the answer @ phobia82

 function increment($string){ $last_char=substr($string,-1); $rest=substr($string, 0, -1); switch ($last_char) { case '': $next= 'A'; break; case 'Z': $next = '0'; $unique = increment($rest); $rest = $unique; break; case '9': $next= 'A'; break; default: $next = ++$last_char; break; } $string=$rest.$next; return $string; } 
0
source share

All Articles