PHPackages                             bentools/string-combinations - 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. bentools/string-combinations

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

bentools/string-combinations
============================

A simple, low-memory footprint function to generate all string combinations from a series of characters.

1.4(11mo ago)31206.9k↓47.4%6MITPHPPHP &gt;=7.4CI passing

Since Jun 22Pushed 11mo ago3 watchersCompare

[ Source](https://github.com/bpolaszek/string-combinations)[ Packagist](https://packagist.org/packages/bentools/string-combinations)[ RSS](/packages/bentools-string-combinations/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (5)Versions (7)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/b3cca79cc0a6970cefb05b112e8c3960ecbf3735feb799e61e826d67c46001c2/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f737472696e672d636f6d62696e6174696f6e732f762f737461626c65)](https://packagist.org/packages/bentools/string-combinations)[![License](https://camo.githubusercontent.com/342273a473523c80a464211379030db1f8dad15681ec674cacf961b4c1dd7c1c/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f737472696e672d636f6d62696e6174696f6e732f6c6963656e7365)](https://packagist.org/packages/bentools/string-combinations)[![CI Workflow](https://github.com/bpolaszek/string-combinations/actions/workflows/ci-workflow.yml/badge.svg)](https://github.com/bpolaszek/string-combinations/actions/workflows/ci-workflow.yml)[![Coverage](https://camo.githubusercontent.com/45b4b07cf01db6635e7b7dcb70105dab50698e73d5b460b0bccd6e7718f1b03f/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f737472696e672d636f6d62696e6174696f6e732f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d43463844524939584457)](https://codecov.io/gh/bpolaszek/string-combinations)[![Quality Score](https://camo.githubusercontent.com/77d8b87ff64b85f070028da6300594b886f3c1d23e00e240c438d75038aafbb3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62706f6c61737a656b2f737472696e672d636f6d62696e6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bpolaszek/string-combinations)[![Total Downloads](https://camo.githubusercontent.com/ef1c61e89e8d74cfeb8ad6e73126da7944534abdc0b106c857eafc70b6265a26/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f737472696e672d636f6d62696e6174696f6e732f646f776e6c6f616473)](https://packagist.org/packages/bentools/string-combinations)

String combinations
===================

[](#string-combinations)

A simple, low-memory footprint function to generate all string combinations from a series of characters.

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

[](#installation)

PHP 7.4+ is required.

> composer require bentools/string-combinations

Usage
-----

[](#usage)

I want to get all combinations with the letters `a`, `b`, `c`.

```
require_once __DIR__ . '/vendor/autoload.php';
use function BenTools\StringCombinations\string_combinations;

foreach (string_combinations('abc') as $combination) { // Can also be string_combinations(['a', 'b', 'c'])
    echo $combination . PHP_EOL;
}
```

Output:

```
a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

```

**Array output**

*It will dump all combinations into an array.*

```
var_dump(string_combinations('abc')->asArray());
```

**Count combinations**

*It will return the number of possible combinations.*

```
var_dump(count(string_combinations('abc'))); // 39
```

**Specifying min length, max length**

```
foreach (string_combinations('abc', $min = 2, $max = 2) as $combination) {
    echo $combination . PHP_EOL;
}
```

Output:

```
aa
ab
ac
ba
bb
bc
ca
cb
cc

```

**Using an array as first argument, and a separator**

```
foreach (string_combinations(['woof', 'meow', 'roar'], 2, 3, '-') as $combination) {
    echo $combination . PHP_EOL;
}
```

Output:

```
woof-woof
woof-meow
woof-roar
meow-woof
meow-meow
meow-roar
roar-woof
roar-meow
roar-roar
woof-woof-woof
woof-woof-meow
woof-woof-roar
woof-meow-woof
woof-meow-meow
woof-meow-roar
woof-roar-woof
woof-roar-meow
woof-roar-roar
meow-woof-woof
meow-woof-meow
meow-woof-roar
meow-meow-woof
meow-meow-meow
meow-meow-roar
meow-roar-woof
meow-roar-meow
meow-roar-roar
roar-woof-woof
roar-woof-meow
roar-woof-roar
roar-meow-woof
roar-meow-meow
roar-meow-roar
roar-roar-woof
roar-roar-meow
roar-roar-roar

```

**No duplicates**

You can avoid generating stings having the same letter more than once with the `withoutDuplicates()` method:

```
$combinations = string_combinations('abc');
var_dump(count($combinations)); // 39
print_r($combinations->asArray());
```

> Array ( \[0\] =&gt; a \[1\] =&gt; b \[2\] =&gt; c \[3\] =&gt; aa \[4\] =&gt; ab \[5\] =&gt; ac \[6\] =&gt; ba \[7\] =&gt; bb \[8\] =&gt; bc \[9\] =&gt; ca \[10\] =&gt; cb \[11\] =&gt; cc \[12\] =&gt; aaa \[13\] =&gt; aab \[14\] =&gt; aac \[15\] =&gt; aba \[16\] =&gt; abb \[17\] =&gt; abc \[18\] =&gt; aca \[19\] =&gt; acb \[20\] =&gt; acc \[21\] =&gt; baa \[22\] =&gt; bab \[23\] =&gt; bac \[24\] =&gt; bba \[25\] =&gt; bbb \[26\] =&gt; bbc \[27\] =&gt; bca \[28\] =&gt; bcb \[29\] =&gt; bcc \[30\] =&gt; caa \[31\] =&gt; cab \[32\] =&gt; cac \[33\] =&gt; cba \[34\] =&gt; cbb \[35\] =&gt; cbc \[36\] =&gt; cca \[37\] =&gt; ccb \[38\] =&gt; ccc )

```
$combinations = $combinations->withoutDuplicates();
var_dump(count($combinations)); // 15
print_r($combinations->asArray());
```

> Array ( \[0\] =&gt; a \[1\] =&gt; b \[2\] =&gt; c \[3\] =&gt; ab \[4\] =&gt; ac \[5\] =&gt; ba \[6\] =&gt; bc \[7\] =&gt; ca \[8\] =&gt; cb \[9\] =&gt; abc \[10\] =&gt; acb \[11\] =&gt; bac \[12\] =&gt; bca \[13\] =&gt; cab \[14\] =&gt; cba )

Performance considerations
--------------------------

[](#performance-considerations)

Because it uses [Generators](http://php.net/manual/en/language.generators.syntax.php) to generate all possible combinations, you can iterate over thousands of possibilities without losing a single MB.

This has been executed on my Core i7 personnal computer with 8GB RAM:

```
require_once __DIR__ . '/vendor/autoload.php';
use function BenTools\StringCombinations\string_combinations;

$start = microtime(true);
$combinations = string_combinations('abcd1234');
foreach ($combinations as $c => $combination) {
    continue;
}
$end = microtime(true);

printf(
    'Generated %d combinations in %ss - Memory usage: %sMB / Peak usage: %sMB' . PHP_EOL,
    ++$c,
    round($end - $start, 3),
    round(memory_get_usage(true) / 1024 / 1024),
    round(memory_get_peak_usage(true) / 1024 / 1024)
);
```

Output:

> Generated 19173960 combinations in 5.579s - Memory usage: 2MB / Peak usage: 2MB

Tests
-----

[](#tests)

> ./vendor/bin/phpunit

See also
--------

[](#see-also)

- [bentools/cartesian-product](https://github.com/bpolaszek/cartesian-product)
- [bentools/iterable-functions](https://github.com/bpolaszek/php-iterable-functions)

###  Health Score

48

—

FairBetter than 93% of packages

Maintenance50

Moderate activity, may be stable

Popularity45

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% 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 ~737 days

Total

5

Last Release

350d ago

PHP version history (2 changes)1.0PHP &gt;=5.6

1.3PHP &gt;=7.4

### Community

Maintainers

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

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (10 commits)")[![progdog-ru](https://avatars.githubusercontent.com/u/68942249?v=4)](https://github.com/progdog-ru "progdog-ru (2 commits)")

---

Tags

combinationslightweightmanipulationmemoryphpstring

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/bentools-string-combinations/health.svg)

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

###  Alternatives

[leaphly/cart-bundle

Cart Bundle

1014.2k1](/packages/leaphly-cart-bundle)

PHPackages © 2026

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