DomingoSL
DomingoSL

Reputation: 15494

Generate an alphanumeric code from a number in php

I want to asing an 5 letters alphanumeric code to all the user that I have in my database, at the momment the only way to identify my user is by their username or ID field in the MySQL data base. I want to use the ID of the user and then generate a 5 letters/numbers code like;

ID USERNAME   CODE
1  Honf       sdew4
2  Djsoft     hgdTs
3  Hanz       HGSTs

Any suggestion of how to make this on php? Thanks!

Upvotes: 0

Views: 714

Answers (3)

Emil Vikström
Emil Vikström

Reputation: 91963

You can do this with base_convert if you want the code to relate to a specific id (this will only use lowercase characters and numbers):

$id = 12;
$code = base_convert($id, 10, 36);
//padding to specified length
$code = str_pad($code, 5, '0', STR_PAD_LEFT);

Upvotes: 3

ahmet alp balkan
ahmet alp balkan

Reputation: 45242

You can use a hashing algorithm such that

substr(strrev(md5($username)),0,5)

This will return hex chars. But please note that if you use A-Z, a-z, 0-9 for your hash domain, if you have many users, there may be collisions and those hashes "may not" be unique.

Upvotes: 2

Pushpendra
Pushpendra

Reputation: 4382

rand_uniqid() function of php may help you.....

rand_uniqid(9007199254740989); will return 'PpQXn7COf' and:
rand_uniqid('PpQXn7COf', true); will return '9007199254740989'

Upvotes: 2

Related Questions