Copyright © Stillat.com—A site by John Koster

The random helper method generates a random string of the specified $length. This method internally uses the OpenSSL function openssl_random_pseudo_bytes, and therefore requires the OpenSSL extension to be installed and configured.

Signature

The signature of the random method is:

public static function random(
        $length = 16
    );

Example Use

It should also be noted that the random method remove the following characters:

Character
/
+
=

RuntimeException

A RuntimeException will be thrown if a call is made to random without the OpenSSL extension installed.

The following examples show sample output. Because the intention of the random method is to generate a random string, any output you get will likely be different:

use Illuminate\Support\Str;

// XHJtXFOa5Jt8B48z
echo Str::random();

// z50fdgeBrmoJRBh7
echo Str::random();

// 2bXJNUcZVdtZfUUzbEgfvvaawOCfOgvK
echo Str::random(32);

Global str_random Helper Function

The str_random function is a shortcut to calling Str::random. This function is declared in the global namespace.

John Koster

Join the mailing list to receive awesome blog posts, product updates and more

Cancel Reply

Discussion

To join the discussion leave your name, email and comment below.

Be the first to leave a reply!

Subscribe to the Mailing List

Join the mailing to list to receive product and blog post updates