Drew Bartlett
Drew Bartlett

Reputation: 1891

easy way to create 4-6 character id of varchar format

I need to have a really simple way to make a shortened id that's always unique. For example, something is created and given a normal auto increment id. I need to have an alternative id if you will that's 4-6 characters.

something like a1b2 or abcd. they always need to be unique though. Please help if you can.

Upvotes: 0

Views: 1762

Answers (7)

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

Why not use the insert id as its unique and pad it with 6x0's

<?php 
$value = sprintf("%06s",mysql_insert_id());
?>

Upvotes: 0

roselan
roselan

Reputation: 3775

you can use mysql conv() function on your auto increment primary key, to convert it from base 10 to, say, base 36. Or you can use php base_convert

in sql:

select yourId, conv(yourId, 10, 36) as shortId from yourTable

in php:

$shortId = base_convert($yourId, 10, 36);

here you can test what it would do exactly.

of course, it's completly unique, safe, and reversable ($orignialId = base_convert($shortId, 36, 10), and both mysql and php versions are compatible, as they do exacty the same thing.

numerical auto increment field can go up to 1.6m before requiring 5 characters in base 36, and 60m before requiring 6.

Upvotes: 0

Kimball Robinson
Kimball Robinson

Reputation: 3387

You could write a function to convert the actual auto-generated ID into a base 36 number, represented by a-z and 0-9. This would give 6-character strings up to 36^7 - 1 = 78,364,164,095 entries. Note, however, that the database will skip some integer values for an autogenerated key (eg, with failed inserts, etc).

If you can be case sensitive, you can have 62^7 - 1 = 3,521,614,606,207 possible combinations.

There's source code for arbitrary base conversion, in PHP, at http://www.pgregg.com/projects/php/base_conversion/base_conversion.php?q=2309823&r=&qb=10&rb=60 -- although it's under an open source license. Hopefully if you google around a bit you can find a Creative Commons or other appropriate license. I suggest starting with "arbitrary base conversion php"

Upvotes: 0

The Mask
The Mask

Reputation: 17427

Try this:

$len = rand(3, 4); 
$hash = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),0,$len);

Upvotes: 1

Sudipta Chatterjee
Sudipta Chatterjee

Reputation: 4670

Just hash the autoincrement id itself - http://www.php.net/manual/en/function.hash.php and select the first 6 characters.

Upvotes: 0

Doug Kress
Doug Kress

Reputation: 3537

uniqid() has worked well for me (http://php.net/manual/en/function.uniqid.php). It returns a hex string that can be turned into a binary string of about 6-7 bytes.

Upvotes: 0

profitphp
profitphp

Reputation: 8354

substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5);

The only way to test if its unique is to check against your database. The possible space with only 4-6 char long strings is relatively small, collisions are quite likely.

Upvotes: 1

Related Questions