Get a random alphanumeric string using PHP

[En Español: https://josemmsimo.wordpress.com/2013/01/18/obteniendo-una-cadena-alfanumerica-usando-php]

In this post, I write how is possible generate an aleatory code (with numbers, letters and symbols) using PHP, easy peasy :-) My company ask me this one, for a specific task, and I think that this one could be useful for generate passwords aleatory:

function getRandomCode(){
	$an = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-)(.:,;";
	$su = strlen($an) - 1;
	return substr($an, rand(0, $su), 1) .
			substr($an, rand(0, $su), 1) .
			substr($an, rand(0, $su), 1) .
			substr($an, rand(0, $su), 1) .
			substr($an, rand(0, $su), 1) .
			substr($an, rand(0, $su), 1);
}

This php code generate 6 aleatory digits. For this one, use:

  • rand(X, Y) function: X is the number more small to generate and Y the number more big. With this one, we will have an aleatory number.
  • substr(X, Y, Z) function: X is the string, Y is where start the string, and Z is the number of letters. With this one, we will have one digit
  • strlen(X) function: X is a string. With this one, we will have the length of the string.

We could be call 100 times to this function so:

for ($i = 0; $i < 100; $i++)
	echo getRandomCode() . "<br />";

And with a result similar to:
CSHY7S
SGYVAQ
YVU(W7
3MYKSW
WF0FWK
DOP1MU
IO-H3S …

4 comentarios en “Get a random alphanumeric string using PHP

  1. Hello ! This is an old post but it’s still useful for persons like me who’s cannot code themeselves, but can trick a little piece of code for their purpose.
    For my html form, I want to auto-generate a random 5 alphanumeric code starting with 2 letters and 3 numbers that will prepopulate a text field when my form loads. Can you help me to achieve this ? I’ve googled but found nothing about auto-filling a form field with an ordonned random string.

    Me gusta

  2. Hello Serge,
    I don’t understand very good your idea.
    If you want generate 5 alphanumeric code, call to the function 5 times:

    for ($i = 0; $i < 100; $i++)
    echo getRandomCode() . "
    «;

    And if you want that the code starts always with 3 specific numbers or 2 specific letters (for example 6, 7, 8, A, B), you could create other string of possibilites:

    function getRandomCode(){
    $intro = «678AB»;
    $an = «0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ»;
    $su = strlen($an) – 1;
    return substr($intro, rand(0, $su), 1) .
    substr($an, rand(0, $su), 1) .
    substr($an, rand(0, $su), 1) .
    substr($an, rand(0, $su), 1) .
    substr($an, rand(0, $su), 1) .
    substr($an, rand(0, $su), 1);
    }

    So, you could have:
    7KJQ2U
    AA24ED
    B2KJS5

    :)

    Me gusta

  3. Thanks Jose for your prompt answer.
    I’ll be more illustrative. I need to ramdomly generate strings like the below that will auto-populate a form field on page load :

    (2 random letters followed by 3 random numbers)

    – FG7895
    – DE451
    – TY321
    – ZK952
    -…

    For now, I found a javascript function that was easy for me to understand and use. It can generate the 5 digits random strings and display them in my field, but they are not syntaxed the way I need, i.e, 2 letters followed by 3 numbers. Please find the script I tested (found on google)
    ————
    function randomString() {
    var chars = «123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ»;
    var string_length = 5;
    var randomstring = »;
    for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
    }
    document.formElem.NameOfMyFormField.value = randomstring;
    }
    —————-
    And I added in the page body tag :

    ————-
    Thank you for your help, Jose

    Me gusta

  4. Hi again,
    Well, you need know that JavaScript is not the same that PHP: PHP is running in the server and JavaScript in the browser of the final client. So, the sintax is a bit different. A simple example for JavaScript of this code is this:

    <!–

    function randomString() {
    var letters = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    var numbers = "123456789";
    var numLetters = 2;
    var numNumbers = 3;
    var randomstring = "";
    for (var i=0; i<numLetters; i++) {
    var rnum = Math.floor(Math.random() * letters.length);
    randomstring += letters.substring(rnum, rnum+1);
    }
    for (var i=0; i<numNumbers; i++) {
    rnum = Math.floor(Math.random() * numbers.length);
    randomstring += numbers.substring(rnum, rnum+1);
    }
    return randomstring;
    }

    function timesForCall(times) {
    var completeText = "";
    for (var i=0; i < times; i++) {
    completeText += randomString() + "
    «;
    }
    return completeText;
    }

    document.body.innerHTML = timesForCall(10);

    //–>

    If you have some question I can try to resolve it :)
    I recommend this page for practice code: http://www.codecademy.com/
    Enjoy the code!

    Me gusta

Deja un comentario

Este sitio utiliza Akismet para reducir el spam. Conoce cómo se procesan los datos de tus comentarios.