Link to home
Start Free TrialLog in
Avatar of parlays
parlays

asked on

How to turn a large number into a shorter one with letters

Hello,

I'm generating an order number based on the unix timestamp and a random number added to the end to make sure that it is a globally unique number.

The number ends up being really long and I would like to put some letters in it.

Is there a good way to take the generated numbers and convert it to something shorter with letters in it.  Or a better way to generate a unique identifier with letters.  I used the unix time since it makes sure that all orders have a different number.

Here is a sample number:
6134713354211947

thanks!!
SOLUTION
Avatar of kiwistag
kiwistag
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what about creating an auto incrementing primary key in the database to use as the order number. this will always be unique.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of parlays
parlays

ASKER

thanks so much everyone!
OK, glad you got something to work, After I posted here last time, I got to consider that the mt_rand( ) is seeded with a server time stamp, so It likely will not be different with the same microtime( ), and according to some, it is possible in muti-processor servers to get the same microtime( ) , , so I did this to get a unique ID, instead of any call to get a random, I get the IP from $_SERVER['REMOTE_ADDR'], which will probally be different if the microtime( ) is identical.  AND I looked up a neat function to shorten an integer string, it's -
base_convert($uID, 10, 36);
you might use it to get a shorter string for integer strings.

below is the code for the makeUID($noNum = false)  function, which always outputs an eleven character string.
if you set the $noNum  to true it will have NO integers in the string just a-z in lower and Caps

function makeUID($noNum = false) {
	$ip = $_SERVER['REMOTE_ADDR'];
	if (strlen($ip)<2) $ip = '91'; else {
		$ip = str_replace('.', '', $ip);
		$ip=substr($ip,strlen($ip)-2);
		}
	$mt = microtime(false);
	$uID=substr($mt,2,6);
	$mt=substr($mt,13);
	$as4 = (int) $ip{1};
	$as2 = (int) $uID{5};
	$e2 = $as2+$as4;
	$uID=$mt.$ip.$uID;
	$k2 = array('6','3','7','4','9','5','4','8','3','8','7','4','5','9','3','8','6','5','3');
	$uID{0}= $k2[$e2];
	$uID=base_convert($uID, 10, 36);
	if ($noNum) $uID = strtr($uID, '01234567890', 'VBMDPFUHWR');
	return $uID;
}

$uID = makeUID();
$uID2 = makeUID(true);
echo strlen($uID),'=length, uID= ',$uID,' , uID2= ',$uID2,'<br />';

Open in new window


I believe this will have a very very high probability to generate unique strings
Avatar of parlays

ASKER

You are the man!  I appreciate you taking the time with those write-ups, I learned a lot.  Thanks!!