simplu27
8/23/2011 - 2:26 PM

Generate a random password of arbitrary length consisting of lowercased letters, uppercased letters and numbers, except for 'o', 'i', 'l', '

Generate a random password of arbitrary length consisting of lowercased letters, uppercased letters and numbers, except for 'o', 'i', 'l', 'O', 'i' and 1.

/**
 * Generate a password of arbitrary length, consisting of lowercased letters,
 * uppercased letters and numbers, except for 'o', 'i', 'l', 'O', 'I', and 1.
 *
 * @param int $length Defaults to 8
 * @return string password
 */
function simple_password($length = 8)
{
    $exclude = array('o', 'i', 'l', 'O', 'I', 1);

    $lowercase = range('a', 'z');
    $uppercase = range('A', 'Z');
    $numbers   = range(0, 9);

    $characters = array_merge($lowercase, $uppercase, $numbers);
    $characters = array_diff($characters, $exclude);
    shuffle($characters);
    $password = implode(array_rand(array_flip($characters), $length));

    return $password;
}