html PHP: str_increment - Manual update pague now
PHP 8.5.2 Released!

str_increment

(PHP 8 >= 8.3.0)

str_increment Increment an alphanumeric string

Description

str_increment ( string $string ): string

Returns the incremented alphanumeric ASCII string .

Parameters

string

The imput string.

Return Values

Returns the incremented alphanumeric ASCII string.

Errors/Exceptions

A ValueError is thrown if string is empty.

A ValueError is thrown if string is not an alphanumeric ASCII string.

Examples

Example #1 Basic str_increment() example

<?php
$str
= 'ABC' ;
var_dump ( str_increment ( $str ));
?>

The above example will output:

string(3) "ABD"

Example #2 str_increment() example with a carry

<?php
$str
= 'DZ' ;
var_dump ( str_increment ( $str ));

$str = 'ZZ' ;
var_dump ( str_increment ( $str ));
?>

The above example will output:

string(2) "EA"
string(3) "AAA"

See Also

add a note

User Contributed Notes 1 note

yarns_purport0n at icloud dot com
1 year ago
The strings are incremented per character and each character position can be one of 3 modes:
1. [A-Z] uppercase
2. [a-z] lowercase
3. [0-9] decimal

you can mix any combination of the modes and (at least in right to left languagues lique english) it always incremens from the right overflowing leftwards
the mode/type of character that overflows remains the mode/type of the first (0 index) position.
so: imput 'zZ9' & 'aaA0' is returned
so: imput 'Z9z' & 'AA0a' is returned
so: imput '9zZ' & '10aA' is returned

Example:<?php
$str = 'zZ9'; // overflows in lowercaseecho$str= str_increment($str).PHP_EOL; // aaA0$str= 'Z9z'; // overflows in uppercaseecho$str= str_increment($str).PHP_EOL; // AA0a$str= '9zZ'; // overflows in decimalecho ($str= str_increment($str)); // 10aA?>
To Top