How to map (large) integer on (small in size( alphanumeric string with PHP? (Cantor?)

Posted by Glooh on Stack Overflow See other posts from Stack Overflow or by Glooh
Published on 2010-06-08T12:00:48Z Indexed on 2010/06/08 12:02 UTC
Read the original article Hit count: 244

Filed under:
|
|
|
|

Dear all,

I can't figure out how to optimally do the following in PHP: In a database, I have messages with a unique ID, like 19041985. Now, I want to refer to these messages in a short-url service but not using generated hashes but by simply 'calculate' the original ID.

In other words, for example: http://short.url/sYsn7 should let me calculate the message ID the visitor would like to request.

To make it more obvious, I wrote the following in PHP to generate these 'alphanumeric ID versions' and of course, the other way around will let me calculate the original message ID.

The question is: Is this the optimal way of doing this? I hardly think so, but can't think of anything else.

$alphanumString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'; for($i=0;$i < strlen($alphanumString);$i++) { $alphanumArray[$i] = substr($alphanumString,$i,1); }

$id = 19041985;

$out = ''; for($i=0;$i < strlen($id);$i++) {

if(isset($alphanumString["".substr($id,$i,2).""]) && strlen($alphanumString["".substr($id,$i,2).""]) > 0) { $out.=$alphanumString["".substr($id,$i,2).""]; } else { $out.=$alphanumString["".substr($id,$i,1).""]; $out.=$alphanumString["".substr($id,($i+1),1).""]; } $i++; }

print $out;

© Stack Overflow or respective owner

Related posts about php

Related posts about string