Jeffrey Kohn - Developer, Cook, Geek

  • Home
  • About Me
  • Blog
    • Sunday Dinners
    • Bacon
    • Beer
    • Scotch
  • Recipes
    • Bacon
    • All Recipes
  • Code
  • Photos
  • Contact
Home

Recent Blog Posts

  • Coins
  • La Trappe - Quadrupel trappist ale
  • Sunday Dinner - Super Bowl
  • Top 5 pictures from my Flickr profile
  • Lunch?
  • Sunday Dinner - Oct 2
  • Sunday Dinner - Sept 25
  • Cheese Steak Stroganoff Sandwich
  • First Jalapeno from the garden
  • Peanut Butter & Bacon Sandwich
more

Recent Recipes

  • Steak & mushroom pie
  • Oven Roasted Pulled Pork Sandwiches
  • Banana Rum Egg Nog
  • bacon wrapped poutine stuffed chicken
  • Ultimate Ice Cream Sandwich
  • Gambas al Ajillo (Garlic Shrimp)
  • Sufganiyot 2
  • Candied Bacon, Maple Whipped Cream Donuts
  • Sufganiyot (Jelly Donuts)
  • French Toast Hot Dog
more

Alpha Numeric Sequences

January 25, 2012

This function allows numbers using any given sequence of numbers or characters. The purpose of this code was to allows us to create larger numbers for row ids in the database but using less number of characters, this might create a larger footprint in the database since the size of one character is 8bits, while an 8bit integers gives you 255 numbers.

in this case the allowed numbers/characters give a numbering system in base62.

this was handy when creating URLs that use the row ID so a quick lookup in the database could be made without a conversion of bases. did not have the conversion functions when originally wrote it, but added them here.

<?php
define
('SEQUENCE', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');

/**
* return next number in sequence base 62
    *
    * @param string
    *     String in base 62
    * @retrun string
    *     String in base 62 incremented by 1
*/
function sequence_next($string)
{
   
$last_char = substr(SEQUENCE, strlen($possible_characters) - 1, 1);
   
$char_position = strlen($string) - 1;
    while (
$char_position >= 0) {
       
$char = substr($string, $char_position, 1);
        if (
$char != $last_char) {
           
$carry_bit = FALSE;
           
$replace_char_pos = strpos(SEQUENCE,$char) + 1;
           
$string = substr_replace($string, substr(SEQUENCE, $replace_char_pos, 1), $char_position, 1);
            return
$string;
        }
        else {
           
$string = substr_replace($string, '0', $char_position, 1);
           
$char_position--;
           
$carry_bit = TRUE;
        }
    }
    if (
$carry_bit = TRUE) {
       
$string = (strlen($string) < 6) ? "1$string" : '1' ;
    }
               
    return
$string;
}

/**
* converts base10 to baseX
    *
    * @param int
    *     String in base10
    * @retrun string
    *     integer baseX
*/
function sequence_base10_to_baseX($int = '')
{
   
$base = strlen(SEQUENCE);
       
   
$string = '';
    if (
$int == 0) return '0';
   
    while (
$int > 0) {
       
$pos = $int % $base;
       
$char = substr(SEQUENCE, $pos, 1);       
       
$int = floor($int/ $base);
       
       
$string = $char . $string;
    }
    return
$string;
}

/**
* converts baseX to genearted base10
    *
    * @param string
    *     String in baseX
    * @retrun int
    *     integer base10
*/
function sequence_baseX_to_base10($string = '')
{
   
   
$str_len = strlen($string);
   
$char_position = $str_len - 1;
   
   
$base = strlen(SEQUENCE);
   
$int = 0;
    while (
$char_position >= 0) {
       
$char = substr($string, $char_position, 1);
       
$char_pos = strpos(SEQUENCE, $char);
       
$int += $char_pos * pow($base, ($str_len - $char_position - 1));
       
$char_position--;
    }
               
    return
$int;
}

echo
'The Squence: ' . SEQUENCE .'<br/><br/>';
echo
'num: ' . 'abcdefg' .'<br/>';
$base = strlen(SEQUENCE);
$num = 'abcdefg';
$num = sequence_next($num);
echo
'Next id: ' .$num . '<br/><br/>';


$num = '11';
echo
'base' . $base . ': ' . $num . '<br/>';
$num = sequence_baseX_to_base10($num);
echo
'base10: ' . $num . '<br/><br/>';

$num = '7000000';
echo
'base10: ' . $num . '<br/>';
$num = sequence_base10_to_baseX($num);
echo
'base' . $base . ': ' . $num . '<br/><br/>';
?>

outputs:

The Squence: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

num: abcdefg
Next id: abcdefh

base62: 11
base10: 63

base10: 7000000
base62: TN1E
  • alphanumeric
  • php
  • sequences
  • Add new comment

Twitter
Facebook
Flickr

  • home
  • about me
  • blog
  • recipes
  • code
  • photos
  • contact