Skip to content

Instantly share code, notes, and snippets.

@nadeem-khan
Last active August 29, 2015 14:18
Show Gist options
  • Save nadeem-khan/66ed3469b89ebaa85b5c to your computer and use it in GitHub Desktop.
Save nadeem-khan/66ed3469b89ebaa85b5c to your computer and use it in GitHub Desktop.
Generate Strong Password with PHP

Generate Strong Password with PHP

    $length = 8;
    $characters_pool = 'luds';
    $sets = array();
    if (strpos($characters_pool, 'l') !== false) {
        $sets[] = 'abcdefghjkmnpqrstuvwxyz';
    }
    if (strpos($characters_pool, 'u') !== false) {
        $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
    }
    if (strpos($characters_pool, 'd') !== false) {
        $sets[] = '123456789';
    }
    if (strpos($characters_pool, 's') !== false) {
        $sets[] = '_-=+./\",|:;!@#$%&*?';
    }
    $all = '';
    $password = '';
    foreach ($sets as $set) {
        $password .= $set[array_rand(str_split($set))];
        $all .= $set;
    }
    $all = str_split($all);
    for ($i = 0; $i < $length - count($sets); $i++) {
        $password .= $all[array_rand($all)];
    }
    echo str_shuffle($password);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment