Download the PHP package bentools/string-combinations without Composer
On this page you can find all versions of the php package bentools/string-combinations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bentools/string-combinations
More information about bentools/string-combinations
Files in bentools/string-combinations
Package string-combinations
Short Description A simple, low-memory footprint function to generate all string combinations from a series of characters.
License MIT
Informations about the package string-combinations
String combinations
A simple, low-memory footprint function to generate all string combinations from a series of characters.
Installation
PHP 7.4+ is required.
composer require bentools/string-combinations
Usage
I want to get all combinations with the letters a
, b
, c
.
Output:
Array output
It will dump all combinations into an array.
Count combinations
It will return the number of possible combinations.
Specifying min length, max length
Output:
Using an array as first argument, and a separator
Output:
No duplicates
You can avoid generating stings having the same letter more than once with the withoutDuplicates()
method:
Array ( [0] => a [1] => b [2] => c [3] => aa [4] => ab [5] => ac [6] => ba [7] => bb [8] => bc [9] => ca [10] => cb [11] => cc [12] => aaa [13] => aab [14] => aac [15] => aba [16] => abb [17] => abc [18] => aca [19] => acb [20] => acc [21] => baa [22] => bab [23] => bac [24] => bba [25] => bbb [26] => bbc [27] => bca [28] => bcb [29] => bcc [30] => caa [31] => cab [32] => cac [33] => cba [34] => cbb [35] => cbc [36] => cca [37] => ccb [38] => ccc )
Array ( [0] => a [1] => b [2] => c [3] => ab [4] => ac [5] => ba [6] => bc [7] => ca [8] => cb [9] => abc [10] => acb [11] => bac [12] => bca [13] => cab [14] => cba )
Performance considerations
Because it uses Generators 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:
Output:
Generated 19173960 combinations in 5.579s - Memory usage: 2MB / Peak usage: 2MB
Tests
./vendor/bin/phpunit