Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function _generate_short_code($id, $length = 6, $randomizer = '171823')
- {
- // Get the randomized number based on $id
- $number = bcmod(bcmul($id, $randomizer), bcpow(10, $length));
- // Make sure number id is long enough and it's length is even
- $length = max(strlen($number), $length) + (max(strlen($number), $length) % 2);
- $number = str_pad($number, $length, '0', STR_PAD_LEFT);
- $vowels = 'aeiou';
- $consonants = 'bcdfghjklmnpqrstvwxyz';
- $short_code = '';
- // Parse the id
- for ($i = 0; $i < $length; $i = $i + 2)
- {
- $slice = substr($number, $i, 2);
- $num = intval($slice);
- $vowel_index = intval(floor($num / strlen($consonants)));
- $consonant_index = $num % strlen($consonants);
- $short_code .= $vowels[$vowel_index].$consonants[$consonant_index];
- }
- return $short_code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement