tags:

views:

136

answers:

5

how can i convert a string(i.e. email address) to unique integers, to use them as an ID.

+1  A: 

Try the binhex function

from the above site:

<?php
$str = "Hello world!";
echo bin2hex($str) . "<br />";
echo pack("H*",bin2hex($str)) . "<br />";
?>

outputs

48656c6c6f20776f726c6421
Hello world!
Preet Sangha
can it generate a unique int now is see it "48656c6c6f20776f726c6421" a hex or so
jspeshu
depends how big the int is though - that has an int value 5216694956355254127
Preet Sangha
+2  A: 

Why not just have an auto-increment ID field on the database?

Spudley
i have no db i'm reading the data from ldap server and the only unique field is their email address
jspeshu
+1 thats the correct approach. no string magic.
nikic
You don't really need a DB for this approach, I guess you could have a text file increment for each category of file name increments.
Vass
i want the persons to update their data(on the ldap server) on using a unique url for a specific user but the same every time the person opens that link. now the only unique thing on the server is their email so i want to convert their email to a unique number so that it will become friendly for the other computations. but now i am considering to use their email in the background no worries about the unique int thing .........thank you all
jspeshu
someone would use a cookie for that? Not unique links, that's a get string really based on the state of the user, right?
Vass
didn't get you ? sorry
jspeshu
+3  A: 

The amount of information a PHP integer may store is limited. The amount of information you can store in a string is not (at least if the string isn't unreasonably long.)

Thus you would need to compress your arbitrary-length string to an non-arbitrary-length integer. This is impossible without data loss.

You may use a hashing algorithm, but hashing algorithms may always have collisions. Especially if you want to hash a string to an integer the collision probability is pretty high - integers can store only very little data.

Thus you shall either stick with the email or use an auto incrementing integer field.

nikic
can you explain your answer better especially the last paragraph and thank you
jspeshu
good reasoning i must say ,but can i @ least find one function that will do this e.g. i have a field i.e. email which is a VARCHAR(40) then since these field is unique for all the values rather than using a hash to avoid collusion, i will add some value say XXXX so that their length == 40 still being unique , then how can i have a unique int out of this
jspeshu
Well. I still don't think you'll find such a function: A 40 character string may be stored in 40 bytes (if it is ascii, unicode will take *even more*). An integer on the other hand only has 8 bytes (and that only on a 64 bit machine). Thus you would still need to store 40 bytes in 8 bytes...
nikic
that is convincing but as they say i want some magic functions thank you very much by the way
jspeshu
A: 

If the emails are ascii text, you could use PHP ord function to generate a unique integer, but it will be a very large number!

The approach would be to work through the email address one character at a time, calling ord for each of them. The ord function returns an integer uniquely expressing the character's value. You can pad each of these numbers with zeros and then use string concatenation to plug them into each other.

Consider "abc".

ord("a");
>> 97

ord("b");
>> 98

ord("c");
>> 99

Pad these numbers with a 0, and you have a unique number for it, that is: 970980990.

I hope that helps!

adamnfish
how about the length limitation e.g. how about this email "[email protected]" is there an equivalent int for this i don't think so b/se php has some constant called PHP_MAX_INT or something like that
jspeshu
Yeah, that'll be the problem to overcome. As everyone is saying, this isn't the right way to do this - I just wanted to highlight an approach in case that helps.
adamnfish
A: 

Why not create your own associative table locally that will bind the emails with unique integers?

So the work flow would be in the lines of:

1   get the record from the ldap server. 
2   check it locally if it has already an int assigned.
2.1 if yes use that int.
2.2 if no, generate an associative row in the table locally.
3   do your things with the unique ids.

Does that make sense?

Slavic
sorry but didn't get that ...
jspeshu
The only way you can maintain an association between your emails and a list of integers is by having a persistent layer (a database for example) that will function as a mapper between your emails and the numbers. So create your database table or file locally and store the data like so: [email protected] = 1001, [email protected] = 1002, etc..
Slavic
that is what i want to avoid and what lead me to this Question? but thank you i think this guy @nikic got it
jspeshu
Understandable, but in that case there's nothing you can do, as there is no such thing that would collisionlessly create an int out of a string for the very reasons nikic stated..
Slavic