Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use an int column in your DB as the primary key that auto-increments on insert, then convert this ID from decimal to base-62 in your logic for the permalink (62 allows use of 0-9, a-z and A-Z).</p> <p>When creating a new permalink:</p> <pre><code>&lt;?php /** * Convert decimal int to a base-62 string * * @param int $dec * @returns string */ function toBase62 ($dec) { // 0 is always 0 if ($dec == 0) return "0"; // this array maps decimal keys to our base-62 radix digits $values = array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ); // convert negative numbers to positive. $neg = $dec &lt; 0; if ($neg) $dec = 0 - $dec; // do the conversion: $chars = array(); // this will store our base-62 chars while ($dec &gt; 0) { $val = $dec % 62; $chars[] = $values[$val]; $dec -= $val; $dec /= 62; } // add zero-padding: while (count($chars) &lt; 6) $chars[] = '0'; // convert to string $rv = implode( '' , array_reverse($chars) ); // if input was negative: return $neg ? "-$rv" : $rv; } // Usage example: // ... do mysql insert here and retrieve new insert_id into var $id ... $permalink = toBase62($id); ?&gt; </code></pre> <p>When decoding a requested permalink:</p> <pre><code>&lt;?php /** * Convert base-62 string to a decimal int * * @param string $str * @returns int on success, FALSE on failure */ function base62ToInt ($str) { // validate str: if ( ! preg_match('/^\-?[0-9A-Za-z]+$/', $str) ) return FALSE; // not a valid string // "0" is always 0 (as is "0000...") if ( preg_match('/^0+$', $str) ) return 0; // this array maps decimal keys to our base-62 radix digits $values = array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ); // flip $values so it maps base-62 digits to decimal values: $values = array_flip($values); // get chars from $str: $chars = str_split($str); // convert negative numbers to positive. $neg = $chars[0] == '-'; if ($neg) array_shift($chars); // do the conversion: $val = 0; $i = 0; while ( count($chars) &gt; 0 ) { $char = array_pop($chars); $val += ($values[$char] * pow(62, $i) ); ++$i; } return $neg ? 0 - $val : $val; } // Usage example: // ... assuming permalink has been put in a var called $permalink $id = base62ToInt($permalink); // ... now look up $id in DB ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload