0

I want to generate a random password that has the following pattern:

Capital, small, number, dash, capital, small, number, special charachter.

Example:

Ab1-Cd2$

I have put all the special characters in an array that I randomly pick, so I have that part figured out. But I have no clue of how to do the other part. Any suggestion?

6
  • 3
    Why does this require a regex? Commented Aug 1, 2011 at 8:16
  • @Mchl The other part as in capital, small, number. And about the other part: I don't know exactly, thought that would be the best solution. Commented Aug 1, 2011 at 8:18
  • If the password is randomly generated, you don't require regex, as the password should be already acceptable to be used, should't it? Commented Aug 1, 2011 at 8:19
  • the other part is to check such a password? Commented Aug 1, 2011 at 8:23
  • its not random if you dictate the pattern, and its also less secure by doing so Commented Aug 1, 2011 at 8:25

3 Answers 3

4

this is not the best way, but this will work

<?php

    $capital    = range ('A','Z');
    $small      = range ('a','z');
    $number     = range ('0','9');
    $special    = array ("#","$","@");

    $password   =   $capital[array_rand($capital)] . 
                    $small[array_rand($small)] . 
                    $number[array_rand($number)] . 
                    "-" . 
                    $capital[array_rand($capital)] . 
                    $small[array_rand($small)] . 
                    $number[array_rand($number)] . 
                    $special[array_rand($special)];

    print $password;

You can read about the functions i used here

array_rand

range

Will produce results like:

Tg8-Im1$
Yj3-Xl3@
Cr1-Lv1@

which i guess is your requirement

Please let me know if you want any more help on this.

Sign up to request clarification or add additional context in comments.

2 Comments

Got it, thanks. But array_rand didnt work for me, it only returned the position as an integer. I went with, for example, rand(0,25).
Yes, array_rand will only return you the position of a random integer. thats why we put it as a key to the same array so that it will return a random value. I tried running that script and it worked for me. Again since we already know the number of elements avilable, your variation is also good. :)
1

Use chr() together with a random ascii value out of the range of A-Z, a-b, 0-9.

chr here: http://php.net/manual/en/function.chr.php

ASCII table here: http://www.asciitable.com/

Example: a-z is from 97 to 122.

Comments

1

You don't need to use regex here. You need an array with values of all possible chars that may appear in the password. Then you simply loop over that array and take random key from it.

$chars = array('a', 'b', 'c'); // For example.
$length = 6;

$password = '';
$count = count($chars) - 1;
for ($i = 0; $i < $length; ++$i) {
    $password .= $chars[mt_rand(0, $count)];
}

echo $password;

Edit:

There's a neat trick if you need values that are somehow in range. You can use range() function.

For example, here are that function, plus, chr() function to get ASCII chars (from 0x20 to 0x7E).

$chars = range(chr(32), chr(126));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.