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

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

forever2077/php-random-string
=============================

1.0.0(2y ago)0231MITPHPPHP ^8.0

Since Aug 20Pushed 2y agoCompare

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

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

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

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

[![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 forever2077/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

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84% 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

1002d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/758b0f41ec6fd34df3b483dd78ceb1f30b4248bfbc1a3733f88ff0669aaddc8e?d=identicon)[forever2077](/maintainers/forever2077)

---

Top Contributors

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

---

Tags

random stringstfn

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[pragmarx/random

Create random chars, numbers, strings

714.2M5](/packages/pragmarx-random)[koala-framework/file-watcher

Library for watching for file system changes

1339.2k1](/packages/koala-framework-file-watcher)[rekurzia/slovak-holidays

Simple PHP library/helper for getting Slovak holidays

114.0k](/packages/rekurzia-slovak-holidays)

PHPackages © 2026

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