Auto-increment alphanumeric characters in PHP
Linc
OwnerDetroit Icrontian
Anyone know how to auto-increment alphanumeric characters for a URL-shortener in PHP?
Instead of counting up from zero, I'd want it to count up from 0-9 and then a-z. Then it would be 00-09 to 0a-0z, and then to 10-19 to 1a-1z, and so on.
Instead of counting up from zero, I'd want it to count up from 0-9 and then a-z. Then it would be 00-09 to 0a-0z, and then to 10-19 to 1a-1z, and so on.
0
Comments
Edit: Now to be serious...
Convert the string to a decimal, add one, then turn it back into ascii...
This might be the long/tedious route, but probably how I'd go about it if you can't find something out there for this already:
Setup a mapping that correlates 0-9a-z to 0-35, maybe in a method that takes a single character and returns the int (sadly this would probably be a 36 case switch statement).
Then break your alphanumeric string into single character and load those into an array. Try to increment the last element of the array by 1 and do a mod operation on the sum by 36 ( newVal = (val +1) % 36). If your newVal is then a 0 (you rolled over), then you need to go back another element in the array and repeat the process. If you roll over all the values in the array, then you tack a 0 on to all the other 0's in the array. Once the operation is done, collect your integers, run them through the mapping in reverse and concat them back into a string.
I presume you're using 0-9a-z because it'll keep the url smaller for a longer duration (versus something like a simple 0-9 system). If you're going for something that, have you considered using UpperCase letters as well, so 0-9a-zA-Z? Giving 62 characters to work with instead of 36.
use an int counter;
then concat all the individual chars
ttttttt-riple post
//edit: shwaip, PHP is super-low-level; there's no strict variable typing, required declarations, or playing with bits. I can't even remember what mod does; I only had an intro C++ course (I assume that's what that was).
mod (%) is basically remainder. 1234 % 1000 = 234;
i re-wrote code below in a better way.
and so on.
the chr() function converts the ascii value to a character
I would say loose typing makes a language more high-level than low-level
[php]
for($counter=0; $counter < HUEG; $counter++){ #this would be the global_index for all your articles
$shortURL ="";
$tmp_count = $counter;
for($i=URL_LEN; $i > 0; $i--){ #URL_LEN is the desired length of url
$tmp = floor($tmp_count/(36^$i));
$tmp_count -= $tmp*(36^$i);
if($tmp < 10) {
$shortURL .= chr($tmp + 48);
} else {
$shortURL .= chr($tmp + 87);
}
}
}
[/php]
I didn't know I had to prescribe the ENTIRE idea and code it for him
So then 36^URL_LEN >= the integer key (HUEG), right? Then I could run this bit before yours:
[php]
$a = HUEG;
$URL_LEN = 1;
while ( $a > 36 ) {
$a = $a/36;
$URL_LEN++;
}[/php]
http://students.washington.edu/hanusaem/test.php
source:
http://students.washington.edu/hanusaem/test.txt
Yes, if you want to have a variable length url, that is how you would do it.
edit: So, this looks like it's working now:
[php]
<?php
for($counter=0; $counter < 10000; $counter+=1) {
$shorturl = "";
$tmp_count = $counter;
for($i=5;$i>=0; $i--) {
$tmp = floor($tmp_count/pow(36,$i));
$tmp_count -= $tmp*pow(36,$i);
if($tmp < 10) {
$shorturl .= chr($tmp + 48);
} else {
$shorturl .= chr($tmp + 87);
}
}
}
?>
[/php]
2. Wow was I ever a newb.