03-09-2008, 06:14 PM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
I made a class a while ago, which generates strings from id's and id's from strings. The coding is pretty crappy, but I was just beginning with php then, so please spare me :).
Here it is:
PHP Code:
<?php
class GenID { // array of usable characters, mix them together to get more random results // you can add uppercase chars to increase the number of possibilities private $useChars = array( 26 => 'a', 11 => 'b', 8 => 'c', 20 => 'd', 10 => 'e', 18 => 'f', 25 => 'g', 3 => 'h', 30 => 'i', 5 => 'j', 32 => 'k', 1 => 'l', 23 => 'm', 14 => 'n', 28 => 'o', 16 => 'p', 33 => 'q', 4 => 'r', 19 => 's', 6 => 't', 21 => 'u', 29 => 'v', 34 => 'w', 24 => 'x', 0 => 'y', 12 => 'z', 27 => 1, 15 => 2, 22 => 3, 9 => 4, 31 => 5, 2 => 6, 17 => 7, 13 => 8, 35 => 9, 7 => 0 ); // reserved id's with the id that generates it as key private $reserved = array( 'home' => 177094, 'index' => 51067968, 'user' => 1004764, 'users' => 36171523, 'admin' => 44634038, 'porn' => 782942, 'about' => 44220282, 'id' => 1100, 'get' => 32766, 'in' => 1094, 'out' => 37050, 'search' => 1166872035 ); private $linkCCount; private static $instance; private function __clone() {} public static function get($id) { if(self::$instance === null) { self::$instance = new GenID; } return self::$instance->run($id); } private function run($id) { if(is_string($id)) { return $this->strtoid($id); } else { $id = intval($id); return $this->idtostr($id); } } private function fixId($id) { if(in_array($id, $this->reserved)) { $nid = $id+1; return $this->fixId($nid); } else { return (int)$id; } } private function clean($url) { return preg_replace('/([\W\s])/', '', $url); } private function idtostr($id, $nofix = false) { if($nofix == false) { $id = $this->fixId($id); } if(!isset($this->linkCCount)) { $this->linkCCount = count($this->useChars); } $this->link = ''; $this->idtostrsub($id); return strrev($this->link); } private function strtoid($str) { if(!isset($this->linkCCount)) { $this->linkCCount = count($this->useChars); } $str = strtolower($str); $str = $this->clean($str); if(strlen($str) === 0) { return false; } elseif(strlen($str) === 1) { return array_search($str, $this->useChars); } $chars = array(); foreach(str_split($str) as $k => $v) { if(in_array($v, $this->useChars)) { $chars[] = array_search($v, $this->useChars); } } $s = $chars[0]; unset($chars[0]); // start the math
foreach($chars as $char) { $s = ($s * $this->linkCCount) + $char; }
return $s; }
private function idtostrsub($id) { $id = (int)$id; // if the id is smaller then the amount of chars just give it a char if($id < $this->linkCCount && $id >= 0) { if($id == $this->linkCCount) $id = 0; $this->link .= $this->useChars[$id]; } else { // devide the id by the amount of chars $flr = floor($id / $this->linkCCount); $res = $id - ($flr * $this->linkCCount); if($res == $this->linkCCount) { $res = 0; } $this->link .= $this->useChars[$res]; $this->link .= ($flr < $this->linkCCount && $flr >= 0)? /* true */ $this->useChars[$flr] : /* false */ $this->idtostrsub($flr); } } } /* Example Usage */ $id = GenID::get('henkifsf'); // result: 257762013534 $str = GenID::get(54646446); // result: ks4pi
?>
It saves a lot of characters, as you can see in the example the id: 257762013534 has the string 'henkifsf'.
|
|
|
|