jspeshu
jspeshu

Reputation: 1211

How to convert a String to a unique INTEGER in php

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

Upvotes: 7

Views: 15846

Answers (8)

SnakeMaster
SnakeMaster

Reputation: 97

You can use crc32 function.

Example:

$email = "[email protected]";
echo $email . " = " . crc32($email);

Live example: https://repl.it/repls/HonorableRespectfulBundledsoftware

Upvotes: 2

Waqar Alamgir
Waqar Alamgir

Reputation: 9968

This code generates 64bit number which can be use as it or as a bigInt / similar data-type for databases like MySQL etc.

function get64BitNumber($str)
{
    return gmp_strval(gmp_init(substr(md5($str), 0, 16), 16), 10);
}

echo get64BitNumber('Hello World!'); // 17079728445181560374
echo get64BitNumber('Hello World#'); // 2208921763183434891
echo get64BitNumber('http://waqaralamgir.tk/'); // 12007604953204508983
echo get64BitNumber('12345678910'); // 4841164765122470932

Upvotes: 2

xoza
xoza

Reputation: 7

You can use this function:

function stringToInteger($string) {
    $output = '';
    for ($i = 0; $i < strlen($string); $i++) {
        $output .= (string) ord($string[$i]);
    }
    return (int) $output;
}

A bit ugly, but works :)

Upvotes: -1

Slavic
Slavic

Reputation: 1962

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?

Upvotes: -1

adamnfish
adamnfish

Reputation: 11255

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!

Upvotes: 0

NikiC
NikiC

Reputation: 101936

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.

Upvotes: 7

Spudley
Spudley

Reputation: 168705

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

Upvotes: 2

Preet Sangha
Preet Sangha

Reputation: 65516

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!

Upvotes: 7

Related Questions