Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique number from a string - php

Tags:

php

I have some strings containing alpha numeric values, say

asdf1234,

qwerty//2345

etc..

I want to generate a specific constant number related with the string. The number should not match any number generated corresponding with other string..

like image 741
Alfred Avatar asked Jun 03 '11 14:06

Alfred


People also ask

How to get unique string in PHP?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.

How to generate unique code in PHP?

A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set. The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy.

How to generate unique alphanumeric code in PHP?

PHP recommends using openssl_random_pseudo_bytes() instead to generate cryptographically secure tokens. which will generate a random string of alphanumeric characters of length = $bytes * 2.


1 Answers

Does it have to be a number?

You could simply hash the string, which would give you a unique value.

echo md5('any string in here');

Note: This is a one-way hash, it cannot be converted from the hash back to the string.

This is how passwords are typically stored (using this or another hash function, typically with a 'salt' method added.) Checking a password is then done by hashing the input and comparing to the stored hash.

edit: md5 hashes are 32 characters in length.

Take a look at other hash functions:
http://us3.php.net/manual/en/function.crc32.php (returns a number, possibly negative)
http://us3.php.net/manual/en/function.sha1.php (40 characters)

like image 174
Fosco Avatar answered Oct 10 '22 01:10

Fosco