-2

Possible Duplicate:
PHP: How to generate a random, unique, alphanumeric string?

I'm creating the image hosting website. I need to create a unique and five letters string (case sensitive) for an image, like an example -> imgur.com/srM0U

I have seen many examples, but they are not unique or not case sensitive. Generated string I will use for an image filename, I think imgur uses a unique strings.

Please help with a piece of code

Community
  • 1
  • 1
michael
  • 129
  • 2
  • 5
  • You are creating an image hosting website and don't know how to create a simple-random-five-characters-length string? – Isaac Sep 18 '11 at 10:13

1 Answers1

0

http://kevin.vanzonneveld.net/techblog/article/create_short_ids_with_php_like_youtube_or_tinyurl/

The main thing to keep in mind is that the way most short-URL systems work is that they save the long form URL into a database, with an auto-incremented ID. That ID is then converted to an alphanumeric short code by a script similar to the one above.

The reason I point this out is I think you're approaching the problem backwards- trying to assign an alphanumeric shortcode first. It's much easier to have unique numeric values (hence, auto-increment) and then approach encoding it from there.

Sorry for the poor formatting, sent from my phone.

Dennis
  • 482
  • 7
  • 17
  • It's difficult code and I need only five letters string – michael Sep 18 '11 at 04:14
  • Sorry, I was going to add some notes about adapting it for your use, but SOing from my phone and it's a pain to type out long explanations- the above code should be a good jumping-off point for you, though. – Dennis Sep 18 '11 at 05:37
  • Is it a good code for getting unique? – michael Sep 18 '11 at 06:17
  • $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $image_name = ''; for($i = 0; $i < 5; $i++) $image_name .= $letters[mt_rand(0, 61)]; – michael Sep 18 '11 at 06:17