TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-09-2008, 06:19 PM   #1 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Terminal Id generator script

I posted this script in a other thread and thought it would be nice to post it here also.

It is a script that converts long id's to strings and the other way. The coding is pretty crappy, but I was just beginning with php then, so please spare me :).

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',
            
=> 'c',
            
20 => 'd',
            
10 => 'e',
            
18 => 'f',
            
25 => 'g',
            
=> 'h',
            
30 => 'i',
            
=> 'j',
            
32 => 'k',
            
=> 'l',
            
23 => 'm',
            
14 => 'n',
            
28 => 'o',
            
16 => 'p',
            
33 => 'q',
            
=> 'r',
            
19 => 's',
            
=> 't',
            
21 => 'u',
            
29 => 'v',
            
34 => 'w',
            
24 => 'x',
            
=> 'y',
            
12 => 'z',
            
27 => 1,
            
15 => 2,
            
22 => 3,
            
=> 4,
            
31 => 5,
            
=> 6,
            
17 => 7,
            
13 => 8,
            
35 => 9,
            
=> 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'.

Hope you guys like it
Attached Files
File Type: php genid.php (3.3 KB, 238 views)
sjaq is offline  
Reply With Quote
The Following User Says Thank You to sjaq For This Useful Post:
KingOfTheSouth (11-30-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:18 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design