StrGen
PHP library for simple, secure random string generation (e.g. - passwords / salts).
Install / Use
/learn @PHLAK/StrGenREADME
Requirements
- PHP >= 7.2
Install with Composer
composer require phlak/strgen
Usage
// 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:
$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<br>ABCDEFGHIJKLMNOPQRSTUVWXYZ |
| StrGen\CharSet::NUMERIC | 0123456789 |
| StrGen\CharSet::ALPHA_NUMERIC | abcdefghijklmnopqrstuvwxyz<br>ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>0123456789 |
| StrGen\CharSet::SPECIAL | !@#$%^&*()-_=+.?{}[]<>:;/\\|~ |
| StrGen\CharSet::ALL | abcdefghijklmnopqrstuvwxyz<br>ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>0123456789<br>!@#$%^&*()-_=+.?{}[]<>:;/\\|~ |
Custom sets:
You can also manually define a character set by passing a string of characters
to the charset() method.
$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.
$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 page.
Troubleshooting
For general help and support join our Spectrum community.
Please report bugs to the GitHub Issue Tracker.
Copyright
This project is licensed under the MIT License.
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
