Anisha Gowda
Anisha Gowda

Reputation: 59

Generate 10 digit number from a string

Hi i have string like below

$string = "hello this is big test string"

or

$string = "hello small";

I need to generate unique 10 numbers from above strings.It don't have to be reversible , but the same unique 10 number should be generated always.

What i tried :

function toNumber($dest)
    {
        if ($dest)
            return ord(strtolower($dest)) - 96;
        else
            return 0;
    }


$str = "hello this is string";
$chars = str_split($str);
foreach($chars as $char){
echo toNumber($char) . " ";
}

P.s

  1. for smaller string less than 10 char also should generate 10 random number
  2. I have huge database of strings , so number should not be dependent on 1st or last 10 char..it should be mixture of all words and unique as possible.

Thanks all.

Upvotes: 0

Views: 2390

Answers (3)

low_rents
low_rents

Reputation: 4481

this is not possible since the english alphabet is consisting of 26 letters and numbers only got 10 "letters".
plus your string also might be longer than 10 chars.
so there is no way to create a unique number with just 10 digits for a string that's between 1 and n long.

for example, let's say your string is 10 characters long like the one in your example:

$string = "hello small";


then you take a simple translation method:

a = 0,  b = 1,  c = 2,  d = 3,  e = 4,  f = 5,  g = 6,  h = 7,  i = 8,  j = 9,  
k = 0,  l = 1,  m = 2,  n = 3,  o = 4,  p = 5,  q = 6,  r = 7,  s = 8,  t = 9,  
u = 0,  v = 1,  w = 2,  x = 3,  y = 4,  z = 5, [space] = 6


then the string translation would be:

74114682011


if you got another string, which could be some name...

$string = "robby imull";


.. it would translate into the exact same digit-code.

(i know this is a strange name but i just did some fast example)

i hope you get what's the problem with your idea of limiting the code to 10 digits.

Upvotes: 1

ankush madaan
ankush madaan

Reputation: 300

This will do

$input = "hello worlds";

$encrypted = encryptIt( $input );

function encryptIt( $q ) {
    $cryptKey  = 'qJB0rGtIn5UB1xG03efyCp';
    $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
    return( $qEncoded );
}
echo substr($encrypted, -20, 10);

Upvotes: 0

Marvin Fischer
Marvin Fischer

Reputation: 2572

make a substr function on an md5 of your string:

substr(hexdec(md5($string)), 0, 10)

Upvotes: 0

Related Questions