tags:

views:

45

answers:

2

I am looking to create a small encrypted string, like the referral strings used by Twitpic or bit.ly, for a website I am working on for referral purposes. Any of the built-in functions like MD5 and mcrypt each make strings that are too long for my purposes.

Is there an easy way to create a string like this? Thanks.

A: 

What about uniqid ? It is not what you really asked, but according to the examples you gave, it could be what you're looking for

Soufiane Hassou
A: 

From the question, I'm guessing that you just want to have a short string of text/numbers that uniquely refers to a username, an image, an URL or something similar.

A solution would be just to generate a random string and map that to the user/image/URL in your database. Here's the random string function we use. You can adjust $chars depending on your application. It can generate a short 5-letter string like twitpic if necessary.

function randstr($length) {
    $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $chooselength = strlen($chars);
    $string = '';
    for ($i = 0; $i < ((int) $length); $i++) {
        $string .= $chars[mt_rand() % $chooselength];
    }
}
phsource