Skip to content

Commit 3e7599e

Browse files
committed
Allow to disable pwd special characters
1 parent 7ff39d5 commit 3e7599e

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ use PH7\Generator\Password;
2424
echo Password::generate(10); // Generate a 10 length password
2525
````
2626

27+
By default, the password will contain special characters. You can disable them by mentioning the second argument to `false`
28+
```php
29+
use PH7\Generator\Password;
30+
31+
// The password won't contain any special characters such as -, _, ~, |, %, ^, !, $, #, @, and ?
32+
echo Password::generate(Password::DEFAULT_LENGTH, false);
33+
````
34+
2735
You can use the public constant `Password::DEFAULT_LENGTH` which contains `12` as the default value
2836

2937
```php

src/Generator/Password.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ class Password
1616
* Generate password.
1717
*
1818
* @param int $iLength
19+
* @param bool $bSpecialCharacters
1920
*
2021
* @return string
2122
*/
22-
public static function generate($iLength)
23+
public static function generate($iLength = self::DEFAULT_LENGTH, $bSpecialCharacters = true)
2324
{
2425
$sWord = '';
26+
$aSpecialChars = $bSpecialCharacters ? self::SPECIAL_CHARACTERS : [];
27+
2528
$aKeys = array_merge(
2629
range(0, 9),
2730
range('a', 'z'),
2831
range('Z', 'Z'),
29-
self::SPECIAL_CHARACTERS
32+
$aSpecialChars
3033
);
3134

3235
for ($iAmount = 0; $iAmount < $iLength; $iAmount++) {

0 commit comments

Comments
 (0)