Adib
Adib

Reputation: 1334

PHP - Generate a string based on regex

I would like to generate a string based on the regular expression I have.

The purpose of this function is when someone forgets their password, and completes the form for "Email New Password", I generate a random password that will be emailed to them. This new password must satisfy the conditions of my regex.

This is my regex:

'/.*^(?=.{6,})(?=.*[A-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).*$/'

Upvotes: 4

Views: 6984

Answers (2)

Ivan Enderlin
Ivan Enderlin

Reputation: 112

Did you take a look at the Hoa\Regex library? It generates strings based on regular expression with an isotropic random approach.

Upvotes: 2

Jeff Hartman
Jeff Hartman

Reputation: 56

Try this. No need for confusing regex:

function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;
}

Upvotes: 1

Related Questions