-2

How can I generate a random string, 20 characters long, A-Z (no lower case), 0-9 only?

I searched online but most scripts to do this are huge (50+ lines). Is there something simpler (maybe 1 line)?

user2656114
  • 949
  • 4
  • 18
  • 35
  • Scrapping the answer I wrote as it was identical to the one linked here. Oh search boxes are heaven sent – Rottingham Jan 13 '14 at 18:51
  • possible duplicate of [Generating (pseudo)random alpha-numeric strings](http://stackoverflow.com/questions/48124/generating-pseudorandom-alpha-numeric-strings) – Matthew Rapati Jan 13 '14 at 23:20

2 Answers2

3

Will this suffice ?

<?php
echo strtoupper(substr(sha1(uniqid()),0,20));

OUTPUT :

A71463EA326B72981D21
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
3

You could create a string with all the character you want that could be in the random string

$chars = 'ABCDEFG...0123456789';

and then create a for loop that picks a random letter or number in the chars string for 20 timer

$randstring = '';
$string_length;
for ($a = 0; $a < $string_length; $a++) {
  $randstring .= $chars[rand(0, strlen($chars) - 1)];
}

Hope it helped!

Manuel
  • 307
  • 1
  • 3
  • 11