PHPackages                             hemker/random-string - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hemker/random-string

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hemker/random-string
====================

Creation of random strings (e.g. for coupon codes in webshops, hashes for one-time links/passwords, accound codes, etc. pp.)

v1.0(2y ago)06[2 issues](https://github.com/KevinHemker/random-string/issues)MITPHPPHP ^8.0

Since Aug 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/KevinHemker/random-string)[ Packagist](https://packagist.org/packages/hemker/random-string)[ RSS](/packages/hemker-random-string/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependenciesVersions (3)Used By (0)

random-string
=============

[](#random-string)

[![License](https://camo.githubusercontent.com/0dddd1e2f5e1350136327dde1370b6214581073449fbd47c78cc0eb9e10c39a1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4b6576696e48656d6b65722f72616e646f6d2d737472696e67)](https://camo.githubusercontent.com/0dddd1e2f5e1350136327dde1370b6214581073449fbd47c78cc0eb9e10c39a1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4b6576696e48656d6b65722f72616e646f6d2d737472696e67)[![PHP-Versions](https://camo.githubusercontent.com/6662dab88babdf3096748b1059b4acd01ab84690f93c50d162d5466a542cea09/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e302532302d2d253230382e322d626c7565)](https://camo.githubusercontent.com/6662dab88babdf3096748b1059b4acd01ab84690f93c50d162d5466a542cea09/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e302532302d2d253230382e322d626c7565)[![Tests](https://camo.githubusercontent.com/cf9c23471a6f2c30bd02b59edb81124fc85a641e06675c7eaeb792c2b386ea5f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f74657374696e672e796d6c3f6272616e63683d646576656c6f70266c6162656c3d7465737473)](https://camo.githubusercontent.com/cf9c23471a6f2c30bd02b59edb81124fc85a641e06675c7eaeb792c2b386ea5f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f74657374696e672e796d6c3f6272616e63683d646576656c6f70266c6162656c3d7465737473)[![codecov](https://camo.githubusercontent.com/b6b701d9acd6ea02fcea9f35e9b5d69e50a06a85d731304e131589771337356e/68747470733a2f2f636f6465636f762e696f2f6769746875622f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f6272616e63682f646576656c6f702f67726170682f62616467652e7376673f746f6b656e3d58414935575051464848)](https://codecov.io/github/KevinHemker/random-string)[![Type-Coverage](https://camo.githubusercontent.com/6cb01e7a26c5ff1ee896b8cd3b536e067ff14cc74d8736d9bc3b0b65bd70d517/68747470733a2f2f73686570686572642e6465762f6769746875622f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f636f7665726167652e737667)](https://camo.githubusercontent.com/6cb01e7a26c5ff1ee896b8cd3b536e067ff14cc74d8736d9bc3b0b65bd70d517/68747470733a2f2f73686570686572642e6465762f6769746875622f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f636f7665726167652e737667)[![Psalm-Level](https://camo.githubusercontent.com/e195368a29c7d80c50ace512f714ad30bae2906998a9f1d43b9119bbbb1ef2b2/68747470733a2f2f73686570686572642e6465762f6769746875622f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f6c6576656c2e737667)](https://camo.githubusercontent.com/e195368a29c7d80c50ace512f714ad30bae2906998a9f1d43b9119bbbb1ef2b2/68747470733a2f2f73686570686572642e6465762f6769746875622f4b6576696e48656d6b65722f72616e646f6d2d737472696e672f6c6576656c2e737667)

**random-string** is a service to create strings of jumbled chars easily. Some use cases:

- coupon codes in web shops
- hashes for one-time links/passwords
- account codes

Some Features:

- full customizable chars to use
- presets of chars available (digits, BASE16, BASE32...)
- No length limit
- works well with frameworks like Symfony (dependency injection), or standalone

Installation
------------

[](#installation)

Open a command console, enter your project directory and execute:

```
$ composer require hemker/random-string
```

If you don't use Composer, give it a try. It's a reason Composer is the most used package manager for PHP! And it's easy to use. [See composer's web page](https://getcomposer.org/doc/00-intro.md) for more information.

Otherwise, download all source files of this project and include `src/` into your own project at best fitting place.

Usage
-----

[](#usage)

Imagine you want to create a random string of length 6, containing digits only. All you need is to interact with one single class.

```
use Hemker\RandomString\RandomStringGenerator;

$generator = new RandomStringGenerator('0123456789', 6);
$string = $generator->create();
// $string is now something random like '395174'

// and of course... you can change settings whenever you want
$generator->chars('abcdefghijklmnopqrstuvwxyz');
$generator->length(10);
```

Typing all allowed chars is boring, hard to read and error-prone? Of course! Use presets instead:

```
use Hemker\RandomString\CharPreset;
use Hemker\RandomString\RandomStringGenerator

// same as above: CharPreset::LETTER_LOWER_CASE == [a-z]
$string = new RandomStringGenerator(CharPreset::LETTER_LOWER_CASE);
```

You have to create a lot of strings, like 500 voucher codes? Simple as that!

```
use Hemker\RandomString\CharPreset;
use Hemker\RandomString\RandomStringGenerator;

$generator = new RandomStringGenerator(CharPreset::LETTER_UPPER_CASE, 8);
$arrayOfStrings = $generator->create(500);
```

> **Note**
>
> If a negative number or 0 is passed to `__construct()` or `length()` an exception is thrown (of type `Hemker\RandomString\Exception\InvalidArgumentException` which extends `\InvalidArgumentException`).

Contributing
------------

[](#contributing)

You'd like to see a feature? Or you found a bug? Awesome! Open source is fueled by everyone's pieces of improvements. Please feel free to create or comment an issue. Every contribution, no matter how small, is welcome!

You're a programmer? Perfect. Pull requests are appreciated! Note this hints:

- This project uses some tools to ensure code quality (phpunit, php-cs-fixer, psalm).
    - run `./phive.phar install` to download all needed phars to `./tools`
    - run `./tools/composer.phar --run-script check-all` before pushing code

> **Note**See `composer.json` (section scripts) for all available commands

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1063d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5691856?v=4)[Dennis Hemker](/maintainers/hemker)[@hemker](https://github.com/hemker)

---

Top Contributors

[![KevinHemker](https://avatars.githubusercontent.com/u/94571214?v=4)](https://github.com/KevinHemker "KevinHemker (33 commits)")

### Embed Badge

![Health badge](/badges/hemker-random-string/health.svg)

```
[![Health](https://phpackages.com/badges/hemker-random-string/health.svg)](https://phpackages.com/packages/hemker-random-string)
```

###  Alternatives

[mtownsend/progress

A PHP package to determine steps and progress.

22117.8k](/packages/mtownsend-progress)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
