str_increment

(PHP 8 >= 8.3.0)

str_incrementIncrémente une chaîne de caractères alphanumérique

Description

str_increment(string $string): string

Retourne la chaîne de caractères alphanumérique ASCII incrémentée string.

Liste de paramètres

string

La chaîne de caractères à incrémenter.

Valeurs de retour

Retourne la chaîne de caractères alphanumérique ASCII incrémentée.

Erreurs / Exceptions

Une exception ValueError est lancée si string est vide.

Une exception ValueError est lancée si string n'est pas une chaîne alphanumérique ASCII.

Exemples

Exemple #1 Exemple basique de la fonction str_increment()

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

L'exemple ci-dessus va afficher :

string(3) "ABD"

Exemple #2 Exemple de str_increment() avec une retenue

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

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

L'exemple ci-dessus va afficher :

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

Voir aussi

add a note

User Contributed Notes 1 note

up
1
yarns_purport0n at icloud dot com
9 months ago
The strings are incremented per character and each character position can be one of 3 modes:1. [A-Z] uppercase2. [a-z] lowercase3. [0-9] decimalyou can mix any combination of the modes and (at least in right to left languages like english) it always increments from the right overflowing leftwardsthe mode/type of character that overflows remains the mode/type of the first (0 index) position.so: input 'zZ9' & 'aaA0' is returnedso: input 'Z9z' & 'AA0a' is returnedso: input '9zZ' & '10aA' is returnedExample:<?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