PHPackages                             stfn/php-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. stfn/php-random-string

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

stfn/php-random-string
======================

v1.0.0(3y ago)2252↓80%2MITPHPPHP ^8.1

Since Mar 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/stfndamjanovic/php-random-string)[ Packagist](https://packagist.org/packages/stfn/php-random-string)[ Docs](https://github.com/stfn/php-random-string)[ GitHub Sponsors](https://github.com/stfndamjanovic)[ RSS](/packages/stfn-php-random-string/feed)WikiDiscussions main Synced today

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

Random string generator for PHP
===============================

[](#random-string-generator-for-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ca2e3cc596f85bbd290c1646d64796408eda6b2a02c676cd37f10d020578efff/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7374666e2f7068702d72616e646f6d2d737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stfn/php-random-string)[![Tests](https://camo.githubusercontent.com/c8ad5be7516522d8551daf462ea190d201916803c76dd2fac70441a7d37c4021/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7374666e64616d6a616e6f7669632f7068702d72616e646f6d2d737472696e672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/stfndamjanovic/php-random-string/actions/workflows/run-tests.yml)

This package can be used to generate a random string based on your set of characters or predefined ones. You can configure string length, prefix, suffix, and count of strings, or skip some strings under certain conditions...

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

[](#installation)

You can install the package via composer:

```
composer require stfn/php-random-string
```

Usage
-----

[](#usage)

### Basic

[](#basic)

Simple example without any configuration.

```
$string = RandomString::new()->generate(); // Output: RIKdjFzuDaN12RiJ
```

### Length definition

[](#length-definition)

You can control the length of the string. By default, it's 16 characters.

```
$string = RandomString::new(6)->generate(); // Output: dzGcot
```

### Predefined charset

[](#predefined-charset)

If you want to generate a string consisting of numbers only, lowercase letters, or uppercase letters you can use predefined charsets.

```
// Generate string that contains only numbers
$config = StringConfig::make()
            ->numbersOnly();

$string = RandomString::fromConfig($config)->generate(); // Output: 9387406871490781

// Generate string that contains only lowercase letters
$config = StringConfig::make()
            ->lowerCaseOnly();

$string = RandomString::fromConfig($config)->generate(); // Output: hvphyfmgnvbbajve

// Generate string that contains only uppercase letters
$config = StringConfig::make()
            ->upperCaseOnly();

$string = RandomString::fromConfig($config)->generate(); // Output: ZIVSUDQHAMDNQAYV
```

### Custom charset

[](#custom-charset)

Or you can use your custom charset for generating random string.

```
$config = StringConfig::make()
            ->charset("ABCDEFG1234");

$string = RandomString::fromConfig($config)->generate(); // Output: 3B41B32C2A12A3A1
```

### Skipping

[](#skipping)

Sometimes you may want to generate a random string but under certain conditions. For example, give me a string that is not part of this array.

```
$config = StringConfig::make()
            ->numbersOnly()
            ->length(6)
            ->skip(function ($string) {
                return in_array($string, ["025922", "104923"]);
            });

$string = RandomString::fromConfig($config)->generate(); // Output: 083712
```

### Prefix and Suffix

[](#prefix-and-suffix)

If you want to add a prefix or suffix to generated string, you can do it like this.

```
$config = StringConfig::make()
            ->length(6)
            ->prefix("PRE_")
            ->suffix("_AFTER");

$string = RandomString::fromConfig($config)->generate(); // Output: PRE_rkM7Jl_AFTER
```

### Array of random strings

[](#array-of-random-strings)

`RandomString` can generate more than just one string.

```
$config = StringConfig::make()
            ->length(6)
            ->count(3);

$strings = RandomString::fromConfig($config)->generate();

// Output: ["ozBYeT", "BYjCtr", "Sw7O5b"];
```

### Uniqueness

[](#uniqueness)

It may happen (rarely, but it's possible) to have not unique strings in the generated array. If you want to avoid it, just change the config.

```
$config = StringConfig::make()
            ->length(6)
            ->count(3)
            ->unique();

$strings = RandomString::fromConfig($config)->generate();

// Output: ["92ONRj", "Me6oym", "WbBPVc"];
```

### Everything in one line

[](#everything-in-one-line)

You can use the `fromArray` method if you don't want to create 2 objects every time.

```
$string = RandomString::fromArray(['length' => 6, 'charset' => 'ABCD1234'])->generate(); // Output: CCDA1D
```

Testing
-------

[](#testing)

```
composer test
```

Security
--------

[](#security)

While the `RandomString` class is designed to generate random and unpredictable string, it is important to note that it is not a cryptographically secure hash function and should not be used for sensitive applications such as password hashing or cryptographic key generation.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Stefan Damjanovic](https://github.com/stfndamjanovic)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

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

Every ~0 days

Total

2

Last Release

1197d ago

Major Versions

v0.1.0 → v1.0.02023-03-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/ab08845d8fb0e2eb1bc4a304ab3a0919ca2c2a0ae0198689d4744047c6e5c809?d=identicon)[stfndamjanovic](/maintainers/stfndamjanovic)

---

Top Contributors

[![stfndamjanovic](https://avatars.githubusercontent.com/u/22433990?v=4)](https://github.com/stfndamjanovic "stfndamjanovic (21 commits)")

---

Tags

phprandomrandom-generationrandom-stringrandom-string-generatorsrandom stringstfn

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[pragmarx/random

Create random chars, numbers, strings

744.6M5](/packages/pragmarx-random)[stfalcon/tinymce-bundle

This Bundle integrates TinyMCE WYSIWYG editor into a Symfony2 project.

2553.0M24](/packages/stfalcon-tinymce-bundle)[reinink/remember-query-strings

Laravel middleware that automatically remembers and restores query strings.

76660.1k3](/packages/reinink-remember-query-strings)[ademarre/binary-to-text-php

Collection of binary-to-text encoding utilities for PHP. Includes Base32 support and much more.

39172.5k](/packages/ademarre-binary-to-text-php)[diarmuidie/niceid

PHP library to generate short, non-sequential, URL-friendly hashes of incremental IDs. Similar to YouTube (https://www.youtube.com/watch?v=dQw4w9WgXcQ) and Bitly (http://bit.ly/1D0CAzd) URLs.

1557.9k](/packages/diarmuidie-niceid)[corneltek/formkit

242.9k2](/packages/corneltek-formkit)

PHPackages © 2026

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