I need to convert domain names to unique integers, to use them as ID.

Result of CRC32 function are not unique.

Optimized 'base_convert' returns same result for both example.com and ex-ample.com

MD5 is not INTEGER and base_convert(md5($str), 36, 10) will produce very big numbers.

Is there a php or mysql function which can help me?

Thanks for any help!

Comments

JohnForsythe’s picture

You could just assign them sequential numbers, starting with 1. If you have a table for them, use auto-increment.

--
John Forsythe
Drupal Modules - Find the module you need for your project!

cafewebmaster’s picture

Thanks but I dont need 'auto_increment' IDs, I need exactly an Unique-String-to-Integer function.

rernst’s picture

MD5 will not produce a unique string for a any given input. Neither will SHA1. Such is the nature of translating arbitrary-length strings into fixed-length ones.

If it doesn't need the properties of a cryptographic hash, then you could take the domain name and turn each alphabetic character into its octal equivalent in ASCII or Unicode. That would be unique

jscoble’s picture

Are you sure that you need an integer to use as a key for the domain name? In software, there are often many ways to solve the same problem, some better than others.

Using a sequence, as one person mentioned, would be an excellent way to handle this with a few additional changes to handle domain collisions or insure that the domain doesn't already exist, but your going to have to do that regardless of the solution you decide.

As the previous poster mentioned converting the characters to their numeric values and then taking the entire string of digits that that represents is the easiest way to guarantee a unique string to integer function. Because of integer overflow issues, you'd have to store and work with the resulting number as a string and not a number. The displayed information would look like a number to you but the system would be handling it like character data. If your going this route, you might was well use the domain name as the key. It's easier and you are essentially doing the same thing only with more work involved.

If you do insist on a Unique-String-to-Integer function, I would suggest getting to know Hash functions. Hash functions are used mainly to quickly locate a piece of data, check for duplicates, check for similar values, get an index value to a large array and such. It can get complicated so getting a basic understanding of the theory behind them, how to design the algorithms, and how to handle the inevitable collision that results in a hashing function.

There are cryptographic hashing algorithms that do create unique values, unlike MD5.

Personally, I'd either use a sequence or use the domain name as the key but I don't know what your use cases are and other things that are specific to what you are trying to do.

ceejayoz’s picture

Why?

Understanding why you've got this rigid requirement (that isn't easily attainable) might help people help you.