How to Increment Letters Like Numbers in PHP

How to increment letters like numbers in PHP?

Character/string increment works in PHP (though decrement doesn't)

$x = 'AAZ';
$x++;
echo $x; // 'ABA'

Increment character in php

You can convert the character into its ASCII value using ord(), increment/decrement that value then convert it back to a character using chr():

$a = 'a';
$num = 2;
$a = chr(ord($a)+$num);
echo $a;
// outputs c

ord- Return ASCII value of character

chr- Return a specific character

If you want the increment/decrement to wrap around a value that exceeds z then you can use this:

$a = 'a';
$num = 28;
echo chr((26 + (ord($a)+$num-97)) % 26 + 97);
// outputs c

Note: As php modulus of a negative number returns a negative number this will not work or $num values < -26 alternative solution include using gmp_mod as mentioned here.

Hope this helps!

Increment letters like number by certain value in php

Incrementing letters will only work with the ++ incrementor operator, not with + addition.

If you want to continue using an incrementor, you can use a loop:

$column = 'AH';
$step = 7; // number of columns to step by
for($ = 0; $i < $step; $i++) {
$column++;
}

However, PHP's character incrementor won't work backwards, so you couldn't use a negative step value


If you want to use addition rather than a loop, then you need to convert that column to a numeric value, do the addition, then convert back

$column = 'AH';
$step = 7; // number of columns to step by
$columnNumber = PHPExcel_Cell::columnIndexFromString($column) + $step;
$column = PHPExcel_Cell::stringFromColumnIndex($columnNumber - 1);

Which has the added benefit of allowing you to use negative step values

PHP 2 chars decrement (AB - AA)

Here's a decrement function that will work for you:

function decrement($str) {
$index = strlen($str)-1;
$ord = ord($str[$index]);
if ($ord > 65) {
// The final character is still greater than A, decrement
return substr($str, 0, $index) . chr($ord-1);
}
if ($index > 0) {
// Strip the final 2 characters and append a Z
return substr($str, 0, $index-1) . 'Z';
}
// Can't be decremented
return false;
}

https://3v4l.org/WaaKY

Increment Letter Number Combination

Using the basic increment operator ++ will account for the various overflow and icrementing if a string with characters and numbers. So something like

$start = 'AA1AA1';
while ( $start != 'AA1AD2') {
echo ($start++)." ";
}

will output

AA1AA1   AA1AA2   AA1AA3   AA1AA4   AA1AA5   AA1AA6   AA1AA7   AA1AA8   AA1AA9   AA1AB0   AA1AB1   

Increment letters by 2

You could use a function like this one to increment variable as many time as wished :

function increment($val, $increment = 2)
{
for ($i = 1; $i <= $increment; $i++) {
$val++;
}

return $val;
}

Usage :

$var = increment('a', 2);
var_dump($var);

How to increment a string with a custom character set

The way this works is by processing the string from the end, each time you look at a character you check it's position in the array (I use a flipped array as it's more efficient than using array_search() each time). Then if the character is at the end of the array, then set it to the 0th element of the alphabet and increment the next digit to the left. If there is another letter from the alphabet to increment the current value, then just replace it and stop the loop.

The last bit is that if you have processed every character and the loop was still going, then there is a carry - so add the 0th digit to the start.

$characters = ['a', 'b', 'c'];
$string = 'cccc';

$index = array_flip($characters);
$alphabetCount = count($index)-1;
for ( $i = strlen($string)-1; $i >= 0; $i--) {
$current = $index[$string[$i]]+1;
// Carry
if ( $current > $alphabetCount ) {
$string[$i] = $characters[0];
}
else {
// update and exit
$string[$i] = $characters[$current];
break;
}
}
// As reached end of loop - carry
if ( $i == -1 ) {
$string = $characters[0].$string;
}
echo $string;

gives

aaaaa

with

$characters = ['f', 'h', 'z', '@', 's'];
$string = 'ffff@zz';

you get

ffff@z@


Related Topics



Leave a reply



Submit