Unique line number - php

I have some lines containing alphanumeric values, say

asdf1234 ,

qwerty//2345

etc.

I want to create a specific constant number associated with a string. The number must not match the number generated on another line.

+4
source share
4 answers

Is this number necessary?

You can simply write a string that will give you a unique value.

 echo md5('any string in here'); 

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

This is how passwords are usually stored (using a particular hash function, usually with the salt method added). Password verification is then performed by hashing the input and comparing it with the stored hash.

edit: md5 hashes are 32 characters long.

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)

+4
source

You can use the hash function like md5 for example, but this is not very interesting.

Instead, you can turn the string into your own ASCII character sequence (since you said it was alpha-numeric) - this way, it can be easily converted back, corresponds to the length of the string (length * 3, to be exact), it has 0 probability collisions, since it just turns it into another representation, always a number, and this is a little more interesting ... Code example:

 function encode($string) { $ans = array(); $string = str_split($string); #go through every character, changing it to its ASCII value for ($i = 0; $i < count($string); $i++) { #ord turns a character into its ASCII values $ascii = (string) ord($string[$i]); #make sure it 3 characters long if (strlen($ascii) < 3) $ascii = '0'.$ascii; $ans[] = $ascii; } #turn it into a string return implode('', $ans); } function decode($string) { $ans = ''; $string = str_split($string); $chars = array(); #construct the characters by going over the three numbers for ($i = 0; $i < count($string); $i+=3) $chars[] = $string[$i] . $string[$i+1] . $string[$i+2]; #chr turns a single integer into its ASCII value for ($i = 0; $i < count($chars); $i++) $ans .= chr($chars[$i]); return $ans; } 

Example:

 $original = 'asdf1234'; #will echo #097115100102049050051052 $encoded = encode($original); echo $encoded . "\n"; #will echo asdf1234 $decoded = decode($encoded); echo $decoded . "\n"; echo $original === $decoded; #echoes 1, meaning true 
+1
source

You are looking for a hash function like md5 . You probably want to pass the $ raw_output = true parameter to it to access the raw bytes, and then pass them to whatever representation you want to enter the number in.

0
source

The cryptographic hash function will give you a different number for each input line, but this is a fairly large number - 20 bytes in the case of SHA-1, for example. In principle, two lines can have the same hash value, but the likelihood that this will happen is so small that it is considered insignificant.

If you want a smaller number - say, a 32-bit integer - then you cannot use the hash function because the chance of a collision is too high. Instead, you will need to keep a record of all the mappings that you created. Create a database table that associates rows with numbers, and each time you are given a row, find it in the table. If you find it, return the associated number. If not, select a new number that is not used by any of the existing entries, and add a new row and number to the table.

0
source

All Articles