I want to enter a short uniq string identifier using the integer from the MySQL table field (question_id) with Auto Incr.
An example . When the user points to www.something.com/SjBWY → php, you will get an entry with id = 23511;
I want to hide information about the number of questions and enter question_code, which will be displayed from 1 to 1 on question_id using some kind of translation algorithm. I do not want to store question_code in the database, I believe that the MySQL developers are smarter than me and have created a reliable mechanism for generating uniq numbers.
Naive approach: ( http://ideone.com/rK4hzx )
$num = 11231; while($num > 0) { $v = ord( $num % 10); $v += 25; echo chr($v); $num = round($num / 10); } // JLKJJ $result = array_reverse(str_split('JLKJJ')); foreach ($result as $single) { echo chr(ord( $single)-25); } // 11231
Question: Could you offer the best solutions?
The naive weakness of the past:
- I want to use small and large letters
- I want to limit the string length to 5 as much as possible.
- The generated line should not have an obvious sequence. The value 11 should be so far from 12.
EDIT The algorithm should be symmetric, I can translate Int-> String and String-> Int. MD5 and other hash algorithms is only one way, I cannot translate from String-> Int
source share