Recently I needed to created a character sequence in PHP like this: aa, ab, ac and so forth. The range() function can’t handle multiple characters, so I wrote a function to generate the next string in the sequence based on the string sent in. The function “carries the one”, so to speak, meaning the next string after az is ba. After zz comes aa again. You can use any amount of characters, so aaaa will yield aaab.

function get_next_in_sequence($str){$letters=range('a','z');$arr=str_split($str);// Replace each character with numeric equivalentforeach($arras$key=>$char){$arr[$key]=array_search($char,$letters);}$digits=count($arr)-1;// Count digitsfor($i=$digits;$i>-1;$i--){// Starting at the right-most spot, move leftif($i==$digits)// If this is the right most spot$arr[$i]++;// Increment itif($arr[$i]==26){// If this spot has moved past "z"$arr[$i]=0;// Set it to "a"if($i!=0)// Unless it is the left most spot$arr[$i-1]++;// Carry the one to the next spot}}// Rebuild characters from numeric equivalentforeach($arras$key=>$char){$arr[$key]=$letters[$char];}$str=implode($arr);return$str;}

CRUTIAL EDIT:

Thanks to a helpful comment from Daniel, it has been brought to my attention that this is now unnecessary. Use this instead: