Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions alphanumeric-generator.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
<?php
function unique_sequential_alphanumeric_generator($key_value,$key_type=4){

//Key types
if ($key_type==1){
//Assembling array tables with individual characters
$key_array_range = array_merge(range('A','Z'), range('a','z'));
}elseif ($key_type==2){
//Megaupload.com style
//Assembling array tables with individual characters
$key_array_range = array_merge(range('A','Z'), range('0','9'));
}elseif ($key_type==3){
//Assembling array tables with individual characters
$key_array_range = array_merge(range('a','z'), range('0','9'));
}elseif ($key_type==4){
//Assembling array tables with individual characters
$key_array_range = array_merge(range('A','Z'), range('a','z'), range('0','9'));
}elseif ($key_type==5){
//Youtube.com style
//Assembling array tables with individual characters
$key_array_range = array_merge(range('A','Z'), range('a','z'), range('0','9'),array('-','_'));
}else{
die('<div align="center">ERROR: Sorry but the key_type ('.$key_type.') you are trying to use does not exist in unique_sequential_alphanumeric_generator().</div>');
}

// Array character set
$chars = $key_array_range;
// Turn key string to array
$code_array = str_split($key_value);

function anderson_makiyama_get_next_alphanumeric($code){

$chars = str_split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");

$code_array = str_split($code);

//Inicia uma busca pelo próximo caractere passível de incremento, ou seja, diferente de Z
//Note que inicia do último caractere para o primeiro
// Starts searching for the next character capable of increasing, or different from end of array
// Note that initiates the last character to the first
for($i = count($code_array)-1;$i>-1;$i--){

if($code_array[$i] == "Z"){
if($code_array[$i] == end($chars)){

if($i==0){
//Se for igual a Z e for o primeiro caractere, então aumenta o comprimento e zera
// If equal to end of array and is the first character, mental increases the length and zeroes

$code_array = array_fill(0,count($code_array) + 1,0);

Expand All @@ -22,15 +43,16 @@ function anderson_makiyama_get_next_alphanumeric($code){
}else{


if($code_array[$i -1] != 'Z'){
//Se o caractere anterior for diferente de Z, incrementa-o e zera o atual e os subsequentes
//Se o caractere anterior for o primeiro, também funciona, pois incrementa ele e zera os demais
if($code_array[$i -1] != end($chars)){

// If the character is different from previous end of array, increment it and resets the current and subsequent
// If the character is above the first, also works because it increases and the other resets

$code_array[$i -1] = $chars[array_search($code_array[$i -1],$chars) + 1];

for($j = $i; $j < count($code_array); $j++){

$code_array[$j] = 0;
$code_array[$j] = reset($chars);

}

Expand All @@ -41,7 +63,7 @@ function anderson_makiyama_get_next_alphanumeric($code){
}

}else{
//calcula o próximo caractere, ou seja, incrementa o atual
// Calculate the next character, or increments the current

$code_array[$i] = $chars[array_search($code_array[$i],$chars) + 1];

Expand All @@ -64,19 +86,4 @@ function anderson_makiyama_get_next_alphanumeric($code){
}

}

?>














6 changes: 3 additions & 3 deletions examplo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$current_number = "aZ5sTp53x";

// Get Next Number
$next_number = anderson_makiyama_get_next_alphanumeric($current_number);
$next_number = unique_sequential_alphanumeric_generator($current_number);

echo "Current = " . $current_number;
echo "Next = " . $next_number;
Expand All @@ -20,11 +20,11 @@

for($i=0;$i<5000;$i++){

$code = anderson_makiyama_get_next_alphanumeric($code);
$code = unique_sequential_alphanumeric_generator($code);

echo $code; echo "<br>";

}


?>
?>
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Sequential Alphanumeric Generator (v.0.1)
Unique Sequential Alphanumeric Generator (v.0.2)

Want to develope a url shortener or just obtain sequential alphanumeric strings, so this php function will help you.
Want to develope a url shortener or just obtain sequential alphanumeric strings for user passwords, so this php function will help you.

This repository contains the open source Sequential Alphanumeric Generator that allows you to
get the next alphanumeric number passing any alphanumeric number as parameter.
Expand All @@ -20,10 +20,10 @@ have is:
$current_number = "aZ5sTp53x";

// Get Next Number
$next_number = anderson_makiyama_get_next_alphanumeric($current_number);
$next_number = unique_sequential_alphanumeric_generator($current_number);
echo $next_number;


[examples]: https://github.com/admiyn/sequential-alphanumeric-generator/blob/master/examplo.php
[examples]: https://github.com/LukasStribrny/unique_sequential_alphanumeric_generator/edit/patch-1/examplo.php



Expand Down