1

I found a question about making short codes like TinyURL (https://stackoverflow.com/a/960364/1778465), and I am not sure if what I am doing is working.

I have the following test code:

<?php $val = intval('murwaresuperchainreaction', 36); echo $val."\n"; echo base_convert($val, 10, 36) . "\n"; echo "---\n"; $val = intval('murwarebarnstormers', 36); echo $val."\n"; echo base_convert($val, 10, 36) . "\n"; echo "---\n"; $val = intval('murwarenightmare', 36); echo $val."\n"; echo base_convert($val, 10, 36) . "\n"; 

and I am getting these results:

9223372036854775807 1y2p0ij32e8e7 --- 9223372036854775807 1y2p0ij32e8e7 --- 9223372036854775807 1y2p0ij32e8e7 

The question I have, is why are all the results the same? according to the answer I linked to above I should be getting "collision-proof" results, but they are all the same...

2 Answers 2

0

As per the Documentation of intval,

The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

When you try this method with shorter strings, you'll get collision free results. But large strings will return the maximum value. Hence this method is not suitable to create short codes from large strings.

Sign up to request clarification or add additional context in comments.

Comments

0

The value being encoded in the answer you linked is an integer - an ID that references that shortened link's record. By Base 64 or Base 36 encoding the ID, the string is becomes a lot shorter:

echo base_convert(1234567, 10, 36); // output qglj 

intval can then be used to convert the shortened string back to the ID:

echo intval('qglj', 36); // output 1234567 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.