PHPackages                             taranovegor/string-generator - 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. taranovegor/string-generator

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

taranovegor/string-generator
============================

Fast, cryptographically-secure random string generator with predefined ASCII alphabets

v1.0.0(3d ago)00MITPHP &gt;=8.2

Since Jul 7Compare

[ Source](https://github.com/taranovegor/string-generator)[ Packagist](https://packagist.org/packages/taranovegor/string-generator)[ RSS](/packages/taranovegor-string-generator/feed)WikiDiscussions Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

String Generator
================

[](#string-generator)

[![CI](https://github.com/taranovegor/string-generator/actions/workflows/ci.yml/badge.svg)](https://github.com/taranovegor/string-generator/actions/workflows/ci.yml)[![PHPStan Level 9](https://camo.githubusercontent.com/1bc07920f0d36e55c17e1d38b1caa132cc605f51a82b388c962870b9a747b898/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e737667)](https://phpstan.org/)[![PHP 8.2+](https://camo.githubusercontent.com/9051b6544f57608ca691a918b38ad8c567657e0ecb6d4e960be274f287d171d8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322532422d3838393242462e737667)](https://www.php.net/)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](LICENSE)

Fast, cryptographically-secure random string generator for PHP 8.2+.

Zero external dependencies. Uses `random_bytes()` with bit-packing and rejection sampling for efficient, unbiased output.

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

[](#installation)

```
composer require taranovegor/string-generator
```

Quick start
-----------

[](#quick-start)

```
use TaranovEgor\StringGenerator\Alphabet;
use TaranovEgor\StringGenerator\StringGenerator;

$generator = new StringGenerator();

// Default — alphanumeric (a-zA-Z0-9)
$token = $generator->generate(32);

// Digits only
$pin = $generator->generate(6, Alphabet::Digits);

// Letters only
$code = $generator->generate(16, Alphabet::Alpha);

// All printable ASCII
$password = $generator->generate(20, Alphabet::Printable);
```

### All predefined alphabets

[](#all-predefined-alphabets)

CaseCharactersSize`Digits``0-9`10`AlphaLower``a-z`26`AlphaUpper``A-Z`26`Alpha``a-z A-Z`52`Alphanumeric``a-z A-Z 0-9`62`Punctuation`32 punctuation characters (`!"#$%&amp;'()\*+,-./:;&lt;=&gt;?@\[\]^\_{}~`)`Printable`All printable ASCII (33–126)94### Custom alphabet

[](#custom-alphabet)

Pass a string as the second argument — every byte in it becomes a valid character:

```
$generator = new StringGenerator();

// Hex string
echo $generator->generate(32, '0123456789abcdef');

// Base58
echo $generator->generate(22, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');

// Binary-safe — works with any byte values, including null bytes
echo $generator->generate(64, "\x00\x01\x02\x03");
```

> **Note:** the alphabet must contain at least 2 distinct bytes.

Development
-----------

[](#development)

```
composer install

# Run tests
composer test

# Static analysis (PHPStan level 9)
composer analyse
```

License
-------

[](#license)

The scripts and documentation in this project are released under the [MIT License](LICENSE)

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance99

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

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

3d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4e8511596b0f666ab1ce311e5aa92ef5711b7401cac283b4e73681ee762c0e8f?d=identicon)[taranovegor](/maintainers/taranovegor)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/taranovegor-string-generator/health.svg)

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

###  Alternatives

[verbb/tablemaker

Create customizable and user-defined table fields.

40176.8k2](/packages/verbb-tablemaker)[symfony/ux-typed

Typed integration for Symfony

11148.4k2](/packages/symfony-ux-typed)

PHPackages © 2026

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