3v4l.org

run code in 300+ PHP versions simultaneously
<?php $characters = ['a', 'b', 'c']; $string = 'cccc'; $characters = array_flip($characters); $base = count($characters); $chrs_to_ords = array_map( function (string $char) use ($characters) : int { return $characters[$char]; }, preg_split('//u', $string, -1, \PREG_SPLIT_NO_EMPTY) ); $ords = implode('', $chrs_to_ords); $baseN = base_convert($ords, $base, 10); $length = strlen($baseN); $next_number = str_pad($baseN + 1, $length, '0', \STR_PAD_LEFT); $next_number = base_convert($next_number, 10, $base); $characters = array_flip($characters); $next_chars = array_map( function(int $ord) use ($characters) { return $characters[$ord]; }, str_split($next_number) ); $next_string = implode('', $next_chars); var_dump($next_string);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
string(5) "baaaa"

preferences:
124.08 ms | 407 KiB | 5 Q