{"id":16463731,"url":"https://github.com/bpolaszek/string-combinations","last_synced_at":"2025-03-16T18:31:25.946Z","repository":{"id":22030441,"uuid":"95022752","full_name":"bpolaszek/string-combinations","owner":"bpolaszek","description":"A simple, low-memory footprint function to generate all string combinations from a series of characters.","archived":false,"fork":false,"pushed_at":"2022-11-11T06:12:08.000Z","size":21,"stargazers_count":31,"open_issues_count":1,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-27T12:08:40.169Z","etag":null,"topics":["combinations","lightweight","manipulation","memory","php","string"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bpolaszek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-21T16:11:43.000Z","updated_at":"2025-01-23T06:40:23.000Z","dependencies_parsed_at":"2023-01-13T21:48:52.297Z","dependency_job_id":null,"html_url":"https://github.com/bpolaszek/string-combinations","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fstring-combinations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fstring-combinations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fstring-combinations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpolaszek%2Fstring-combinations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bpolaszek","download_url":"https://codeload.github.com/bpolaszek/string-combinations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["combinations","lightweight","manipulation","memory","php","string"],"created_at":"2024-10-11T11:15:21.617Z","updated_at":"2025-03-16T18:31:25.529Z","avatar_url":"https://github.com/bpolaszek.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/bentools/string-combinations/v/stable)](https://packagist.org/packages/bentools/string-combinations)\n[![License](https://poser.pugx.org/bentools/string-combinations/license)](https://packagist.org/packages/bentools/string-combinations)\n[![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)\n[![Coverage](https://codecov.io/gh/bpolaszek/string-combinations/branch/master/graph/badge.svg?token=CF8DRI9XDW)](https://codecov.io/gh/bpolaszek/string-combinations)\n[![Quality Score](https://img.shields.io/scrutinizer/g/bpolaszek/string-combinations.svg?style=flat-square)](https://scrutinizer-ci.com/g/bpolaszek/string-combinations)\n[![Total Downloads](https://poser.pugx.org/bentools/string-combinations/downloads)](https://packagist.org/packages/bentools/string-combinations)\n\n# String combinations\n\nA simple, low-memory footprint function to generate all string combinations from a series of characters.\n\nInstallation\n------------\n\nPHP 7.4+ is required.\n\n\u003e composer require bentools/string-combinations\n\nUsage\n-----\n\nI want to get all combinations with the letters `a`, `b`, `c`.\n\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\nuse function BenTools\\StringCombinations\\string_combinations;\n\nforeach (string_combinations('abc') as $combination) { // Can also be string_combinations(['a', 'b', 'c'])\n    echo $combination . PHP_EOL;\n}\n```\n\nOutput:\n```\na\nb\nc\naa\nab\nac\nba\nbb\nbc\nca\ncb\ncc\naaa\naab\naac\naba\nabb\nabc\naca\nacb\nacc\nbaa\nbab\nbac\nbba\nbbb\nbbc\nbca\nbcb\nbcc\ncaa\ncab\ncac\ncba\ncbb\ncbc\ncca\nccb\nccc\n```\n\n**Array output**\n\n_It will dump all combinations into an array._\n\n```php\nvar_dump(string_combinations('abc')-\u003easArray());\n```\n\n**Count combinations**\n\n_It will return the number of possible combinations._\n\n```php\nvar_dump(count(string_combinations('abc'))); // 39\n```\n\n**Specifying min length, max length**\n\n```php\nforeach (string_combinations('abc', $min = 2, $max = 2) as $combination) {\n    echo $combination . PHP_EOL;\n}\n```\n\nOutput:\n```\naa\nab\nac\nba\nbb\nbc\nca\ncb\ncc\n```\n\n**Using an array as first argument, and a separator**\n\n```php\nforeach (string_combinations(['woof', 'meow', 'roar'], 2, 3, '-') as $combination) {\n    echo $combination . PHP_EOL;\n}\n```\n\nOutput:\n```\nwoof-woof\nwoof-meow\nwoof-roar\nmeow-woof\nmeow-meow\nmeow-roar\nroar-woof\nroar-meow\nroar-roar\nwoof-woof-woof\nwoof-woof-meow\nwoof-woof-roar\nwoof-meow-woof\nwoof-meow-meow\nwoof-meow-roar\nwoof-roar-woof\nwoof-roar-meow\nwoof-roar-roar\nmeow-woof-woof\nmeow-woof-meow\nmeow-woof-roar\nmeow-meow-woof\nmeow-meow-meow\nmeow-meow-roar\nmeow-roar-woof\nmeow-roar-meow\nmeow-roar-roar\nroar-woof-woof\nroar-woof-meow\nroar-woof-roar\nroar-meow-woof\nroar-meow-meow\nroar-meow-roar\nroar-roar-woof\nroar-roar-meow\nroar-roar-roar\n```\n\n**No duplicates**\n\nYou can avoid generating stings having the same letter more than once with the `withoutDuplicates()` method:\n\n```php\n$combinations = string_combinations('abc');\nvar_dump(count($combinations)); // 39\nprint_r($combinations-\u003easArray());\n```\n\n\u003e Array\n  (\n      [0] =\u003e a\n      [1] =\u003e b\n      [2] =\u003e c\n      [3] =\u003e aa\n      [4] =\u003e ab\n      [5] =\u003e ac\n      [6] =\u003e ba\n      [7] =\u003e bb\n      [8] =\u003e bc\n      [9] =\u003e ca\n      [10] =\u003e cb\n      [11] =\u003e cc\n      [12] =\u003e aaa\n      [13] =\u003e aab\n      [14] =\u003e aac\n      [15] =\u003e aba\n      [16] =\u003e abb\n      [17] =\u003e abc\n      [18] =\u003e aca\n      [19] =\u003e acb\n      [20] =\u003e acc\n      [21] =\u003e baa\n      [22] =\u003e bab\n      [23] =\u003e bac\n      [24] =\u003e bba\n      [25] =\u003e bbb\n      [26] =\u003e bbc\n      [27] =\u003e bca\n      [28] =\u003e bcb\n      [29] =\u003e bcc\n      [30] =\u003e caa\n      [31] =\u003e cab\n      [32] =\u003e cac\n      [33] =\u003e cba\n      [34] =\u003e cbb\n      [35] =\u003e cbc\n      [36] =\u003e cca\n      [37] =\u003e ccb\n      [38] =\u003e ccc\n  )\n\n\n```php\n$combinations = $combinations-\u003ewithoutDuplicates();\nvar_dump(count($combinations)); // 15\nprint_r($combinations-\u003easArray());\n```\n\n\u003e Array\n  (\n      [0] =\u003e a\n      [1] =\u003e b\n      [2] =\u003e c\n      [3] =\u003e ab\n      [4] =\u003e ac\n      [5] =\u003e ba\n      [6] =\u003e bc\n      [7] =\u003e ca\n      [8] =\u003e cb\n      [9] =\u003e abc\n      [10] =\u003e acb\n      [11] =\u003e bac\n      [12] =\u003e bca\n      [13] =\u003e cab\n      [14] =\u003e cba\n  )\n\n\nPerformance considerations\n--------------------------\n\nBecause 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.\n\nThis has been executed on my Core i7 personnal computer with 8GB RAM:\n```php\nrequire_once __DIR__ . '/vendor/autoload.php';\nuse function BenTools\\StringCombinations\\string_combinations;\n\n$start = microtime(true);\n$combinations = string_combinations('abcd1234');\nforeach ($combinations as $c =\u003e $combination) {\n    continue;\n}\n$end = microtime(true);\n\nprintf(\n    'Generated %d combinations in %ss - Memory usage: %sMB / Peak usage: %sMB' . PHP_EOL,\n    ++$c,\n    round($end - $start, 3),\n    round(memory_get_usage(true) / 1024 / 1024),\n    round(memory_get_peak_usage(true) / 1024 / 1024)\n);\n```\n\nOutput:\n\u003e Generated 19173960 combinations in 5.579s - Memory usage: 2MB / Peak usage: 2MB\n\n\nTests\n------------\n\n\u003e ./vendor/bin/phpunit\n\n\nSee also\n--------\n\n- [bentools/cartesian-product](https://github.com/bpolaszek/cartesian-product)\n- [bentools/iterable-functions](https://github.com/bpolaszek/php-iterable-functions)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpolaszek%2Fstring-combinations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbpolaszek%2Fstring-combinations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpolaszek%2Fstring-combinations/lists"}