Created
December 16, 2014 15:04
-
-
Save lewayotte/317fd3f82c66026d50e7 to your computer and use it in GitHub Desktop.
Incrementing alphanumeric strings in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function alphanumeric_increment( $string, $position=false ) { | |
if ( false === $position ) { | |
$position = strlen( $string ) - 1; | |
} | |
$increment_str = substr( $string, $position, 1 ); | |
switch ( $increment_str ) { | |
case '9': | |
$string = substr_replace( $string, 'a', $position, 1 ); | |
break; | |
case 'z': | |
$string = substr_replace( $string, 'A', $position, 1 ); | |
break; | |
case 'Z': | |
if ( 0 === $position ) { | |
$string = substr_replace( $string, '0', $position, 1 ); | |
$string .= '0'; | |
} else { | |
$inc_position = $position - 1; | |
$string = increment( $string, $inc_position ); | |
$string = substr_replace( $string, '0', $position, 1 ); | |
} | |
break; | |
default: | |
$increment_str++; | |
$string = substr_replace( $string, $increment_str, $position, 1 ); | |
break; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 19, should be:
Function
increment
is not defined.