https://github.com/phlak/strgen
PHP library for simple, secure random string generation (e.g. - passwords / salts).
https://github.com/phlak/strgen
php php-library random-string
Last synced: 8 months ago
JSON representation
PHP library for simple, secure random string generation (e.g. - passwords / salts).
- Host: GitHub
- URL: https://github.com/phlak/strgen
- Owner: PHLAK
- License: mit
- Created: 2014-05-05T05:47:16.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2023-07-01T11:35:56.000Z (over 2 years ago)
- Last Synced: 2025-06-23T05:06:50.351Z (9 months ago)
- Topics: php, php-library, random-string
- Language: PHP
- Homepage:
- Size: 156 KB
- Stars: 22
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
PHP library for simple secure random string generation (e.g. - passwords / salts)
Created by Chris Kankiewicz (@PHLAK)
---
Requirements
------------
- [PHP](https://php.net) >= 7.2
Install with Composer
---------------------
```bash
composer require phlak/strgen
```
Usage
-----
```php
// Import StrGen
use PHLAK\StrGen;
// Initialize the Generator
$generator = new StrGen\Generator();
// Generate a random string of characters
$generator->length(16)->generate(); // Returns something like '8a*Ag@I0*s0v[S3u'
```
### Character Sets
StrGen has a few built-in character sets available for ease of use. You can
specify which set(s) to use by passing a character set or an array of sets to
the `charset()` method.
**Example using built-in sets:**
```php
$generator = new StrGen\Generator();
$generator->charset(StrGen\CharSet::ALPHA_NUMERIC)->generate();
// or
$generator->charset([StrGen\CharSet::MIXED_ALPHA, StrGen\CharSet::NUMERIC])->generate();
```
**Available presets:**
| Key | Character Set |
| ------------------------------- | ---------------------------- |
| `StrGen\CharSet::LOWER_ALPHA` | `abcdefghijklmnopqrstuvwxyz` |
| `StrGen\CharSet::UPPER_ALPHA` | `ABCDEFGHIJKLMNOPQRSTUVWXYZ` |
| `StrGen\CharSet::MIXED_ALPHA` | `abcdefghijklmnopqrstuvwxyz`
`ABCDEFGHIJKLMNOPQRSTUVWXYZ` |
| `StrGen\CharSet::NUMERIC` | `0123456789` |
| `StrGen\CharSet::ALPHA_NUMERIC` | `abcdefghijklmnopqrstuvwxyz`
`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
`0123456789` |
| `StrGen\CharSet::SPECIAL` | `!@#$%^&*()-_=+.?{}[]<>:;/\\|~` |
| `StrGen\CharSet::ALL` | `abcdefghijklmnopqrstuvwxyz`
`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
`0123456789`
`!@#$%^&*()-_=+.?{}[]<>:;/\\|~` |
**Custom sets:**
You can also manually define a character set by passing a string of characters
to the `charset()` method.
```php
$generator = new StrGen\Generator();
$generator->charset('0123456789abcdef')->generate();
```
### Convenience Functions
StrGen also has built-in convenience functions for generating strings from the
included character sets or a custom character set.
```php
$generator->lowerAlpha($length);
$generator->upperAlpha($length);
$generator->mixedAlpha($length);
$generator->numeric($length);
$generator->alphaNumeric($length);
$generator->special($length);
$generator->all($length);
$generator->custom($length, $charset);
```
Changelog
---------
A list of changes can be found on the [GitHub Releases](https://github.com/PHLAK/StrGen/releases) page.
Troubleshooting
---------------
For general help and support join our [Spectrum community](https://spectrum.chat/phlaknet).
Please report bugs to the [GitHub Issue Tracker](https://github.com/PHLAK/StrGen/issues).
Copyright
---------
This project is licensed under the [MIT License](https://github.com/PHLAK/StrGen/blob/master/LICENSE).