|
| 1 | +PHP - Longest Common Substring |
| 2 | +============================== |
| 3 | + |
| 4 | +PHP implementation of an algorithm to solve the `longest common substring` problem. |
| 5 | + |
| 6 | +[![Latest Version on Packagist][ico-version]][link-packagist] |
| 7 | +[![Pre Release Version on Packagist][ico-pre-release]][link-packagist] |
| 8 | +[![Latest Unstable Version][ico-unstable]][link-packagist] |
| 9 | +[![Build Status][ico-travis]][link-travis] |
| 10 | +[![Coverage status][ico-codecov]][link-codecov] |
| 11 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 12 | +[![The most recent stable version is 2.0.0][ico-semver]][link-semver] |
| 13 | +[![Software License][ico-license]](LICENSE.md) |
| 14 | + |
| 15 | +# About |
| 16 | + |
| 17 | +*PHP-Longest-Common-Subsequence* is a PHP implementation of an algorithm to solve the 'longest common substring' problem. |
| 18 | + |
| 19 | +From [Wikipedia - Longest common substring problem](https://en.wikipedia.org/wiki/Longest_common_substring_problem): |
| 20 | + |
| 21 | +> In computer science, the longest common substring problem is to find the longest string (or strings) that is a |
| 22 | +> substring (or are substrings) of two or more strings. |
| 23 | +
|
| 24 | +# Installation |
| 25 | + |
| 26 | +Require [triun/longest-common-substring package](https://packagist.org/packages/triun/longest-common-substring) with [composer](http://getcomposer.org/) |
| 27 | +using the following command: |
| 28 | + |
| 29 | +```bash |
| 30 | +composer require triun/longest-common-substring |
| 31 | +``` |
| 32 | + |
| 33 | +# Usage |
| 34 | + |
| 35 | +## Solver |
| 36 | + |
| 37 | +```php |
| 38 | +use Triun\LongestCommonSubstring\Solver; |
| 39 | + |
| 40 | +$solver = new Solver(); |
| 41 | + |
| 42 | +$stringA = '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 43 | +$stringB = '56789AB56789ABCDE56789ABCDE56789AB56789A123456789A'; |
| 44 | + |
| 45 | +// calculates the LCSubstring to be '123456789A' |
| 46 | +$result = $solver->solve($stringA, $stringB); |
| 47 | +``` |
| 48 | + |
| 49 | +## Matches solver |
| 50 | + |
| 51 | +```php |
| 52 | +use Triun\LongestCommonSubstring\Solver; |
| 53 | + |
| 54 | +$solver = new Solver(); |
| 55 | + |
| 56 | +$stringA = '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 57 | +$stringB = '56789AB56789ABCDE56789ABCDE56789AB56789A123456789A'; |
| 58 | + |
| 59 | +$matches = $matchSolver->solve($stringA, $stringB); |
| 60 | + |
| 61 | +// calculates the LCSubstring to be '123456789A' |
| 62 | +$result = "$matches"; |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +But also can give you the rest of the results which has the same length: |
| 67 | + |
| 68 | +```php |
| 69 | +var_dump($matches->values()); |
| 70 | +``` |
| 71 | + |
| 72 | +``` |
| 73 | +array:12 [ |
| 74 | + 0 => "123456789A" |
| 75 | + 1 => "56789ABCDE" |
| 76 | + 2 => "56789ABCDE" |
| 77 | + 3 => "123456789A" |
| 78 | + 4 => "56789ABCDE" |
| 79 | + 5 => "56789ABCDE" |
| 80 | + 6 => "123456789A" |
| 81 | + 7 => "56789ABCDE" |
| 82 | + 8 => "56789ABCDE" |
| 83 | + 9 => "123456789A" |
| 84 | + 10 => "56789ABCDE" |
| 85 | + 11 => "56789ABCDE" |
| 86 | +] |
| 87 | +``` |
| 88 | + |
| 89 | +You can use `unique` to skip duplicated values: |
| 90 | + |
| 91 | +```php |
| 92 | +var_dump($matches->unique()); |
| 93 | +``` |
| 94 | + |
| 95 | +``` |
| 96 | +array:2 [ |
| 97 | + 0 => "123456789A" |
| 98 | + 1 => "56789ABCDE" |
| 99 | +] |
| 100 | +``` |
| 101 | + |
| 102 | +Or even more information about the matches, like the input strings indexes: |
| 103 | + |
| 104 | +```php |
| 105 | +var_dump($matches->values()); |
| 106 | +``` |
| 107 | + |
| 108 | +``` |
| 109 | +array:12 [ |
| 110 | + 0 => array:3 [ |
| 111 | + "value" => "123456789A" |
| 112 | + "length" => 10 |
| 113 | + "indexes" => array:2 [ |
| 114 | + 0 => 1 |
| 115 | + 1 => 40 |
| 116 | + ] |
| 117 | + ] |
| 118 | + 1 => array:3 [ |
| 119 | + "value" => "56789ABCDE" |
| 120 | + "length" => 10 |
| 121 | + "indexes" => array:2 [ |
| 122 | + 0 => 5 |
| 123 | + 1 => 7 |
| 124 | + ] |
| 125 | + ] |
| 126 | + 2 => array:3 [ |
| 127 | + "value" => "56789ABCDE" |
| 128 | + "length" => 10 |
| 129 | + "indexes" => array:2 [ |
| 130 | + 0 => 5 |
| 131 | + 1 => 17 |
| 132 | + ] |
| 133 | + ] |
| 134 | + ... |
| 135 | +] |
| 136 | +``` |
| 137 | + |
| 138 | +# Issues |
| 139 | + |
| 140 | +Bug reports and feature requests can be submitted on the |
| 141 | +[Github Issue Tracker](https://github.com/Triun/PHP-Longest-Common-Substring/issues). |
| 142 | + |
| 143 | +# Contributing |
| 144 | + |
| 145 | +See [CONTRIBUTING.md](CONTRIBUTING.md) for information. |
| 146 | + |
| 147 | +# License |
| 148 | + |
| 149 | +This repository is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) |
| 150 | + |
| 151 | +<!-- References --> |
| 152 | + |
| 153 | +[ico-version]: https://img.shields.io/packagist/v/triun/longest-common-substring.svg |
| 154 | +[ico-pre-release]: https://img.shields.io/packagist/vpre/triun/longest-common-substring.svg |
| 155 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 156 | +[ico-travis]: https://travis-ci.org/Triun/PHP-Longest-Common-Substring.svg?branch=master |
| 157 | +[ico-code-quality]: https://img.shields.io/scrutinizer/g/triun/longest-common-substring.svg?style=flat-square |
| 158 | +[ico-downloads]: https://img.shields.io/packagist/dt/triun/longest-common-substring.svg?style=flat-square |
| 159 | +[ico-unstable]: https://poser.pugx.org/triun/longest-common-substring/v/unstable |
| 160 | +[ico-coveralls]: https://coveralls.io/repos/github/Triun/PHP-Longest-Common-Substring/badge.svg?branch=master "Current test coverage for the develop branch" |
| 161 | +[ico-codecov]: https://codecov.io/gh/Triun/PHP-Longest-Common-Substring/branch/master/graph/badge.svg |
| 162 | +[ico-semver]: http://img.shields.io/:semver-2.0.0-brightgreen.svg "This project uses semantic versioning" |
| 163 | + |
| 164 | +[link-packagist]: https://packagist.org/packages/triun/longest-common-substring |
| 165 | +[link-travis]: https://travis-ci.org/Triun/PHP-Longest-Common-Substring |
| 166 | +[link-downloads]: https://packagist.org/packages/triun/longest-common-substring |
| 167 | +[link-author]: https://github.com/Triun |
| 168 | +[link-coveralls]: https://coveralls.io/github/Triun/PHP-Longest-Common-Substring?branch=master |
| 169 | +[link-codecov]: https://codecov.io/gh/Triun/PHP-Longest-Common-Substring |
| 170 | +[link-semver]: http://semver.org/ |
0 commit comments