PHPackages                             triun/longest-common-substring - 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. triun/longest-common-substring

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

triun/longest-common-substring
==============================

PHP implementation of an algorithm to solve the `longest common substring` problem.

1.0.0(8y ago)351.2k↓48.2%1[2 PRs](https://github.com/Triun/PHP-Longest-Common-Substring/pulls)MITPHPPHP &gt;=7.0.0

Since Apr 8Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Triun/PHP-Longest-Common-Substring)[ Packagist](https://packagist.org/packages/triun/longest-common-substring)[ Docs](https://github.com/Triun)[ RSS](/packages/triun-longest-common-substring/feed)WikiDiscussions master Synced 1mo ago

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

PHP - Longest Common Substring
==============================

[](#php---longest-common-substring)

PHP implementation of an algorithm to solve the `longest common substring` problem.

[![Latest Version on Packagist](https://camo.githubusercontent.com/3b0a8b419cf575fdc8eb64a1402044026b80593e8aa4ed84cb5aa17b42f93dd4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747269756e2f6c6f6e676573742d636f6d6d6f6e2d737562737472696e672e737667)](https://packagist.org/packages/triun/longest-common-substring)[![Build Status](https://camo.githubusercontent.com/9ad524d70d2f542aede6fc977a1e257b6e7c2a63703bef6e5d247f3cc4b671ed/68747470733a2f2f7472617669732d63692e6f72672f547269756e2f5048502d4c6f6e676573742d436f6d6d6f6e2d537562737472696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Triun/PHP-Longest-Common-Substring)[![Coverage status](https://camo.githubusercontent.com/cadeaa238bcd877406f7cfe250c6a6acc0da6dc29fbaf9c3bde0ae991adaf46d/68747470733a2f2f636f6465636f762e696f2f67682f547269756e2f5048502d4c6f6e676573742d436f6d6d6f6e2d537562737472696e672f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/Triun/PHP-Longest-Common-Substring)[![Total Downloads](https://camo.githubusercontent.com/8f6f9da3b35bc6f2fd33fbcbf809c627c95c302229f788d6ee2cd529f852632b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f747269756e2f6c6f6e676573742d636f6d6d6f6e2d737562737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/triun/longest-common-substring)[![The most recent stable version is 2.0.0](https://camo.githubusercontent.com/14292701dabbd874c065e08be17dde83a526fb7d21a7874f6d068c1467bc4554/687474703a2f2f696d672e736869656c64732e696f2f3a73656d7665722d322e302e302d627269676874677265656e2e737667 "This project uses semantic versioning")](http://semver.org/)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

About
=====

[](#about)

*PHP-Longest-Common-Subsequence* is a PHP implementation of an algorithm to solve the 'longest common substring' problem.

From [Wikipedia - Longest common substring problem](https://en.wikipedia.org/wiki/Longest_common_substring_problem):

> In computer science, the longest common substring problem is to find the longest string (or strings) that is a substring (or are substrings) of two or more strings.

Installation
============

[](#installation)

Require [triun/longest-common-substring package](https://packagist.org/packages/triun/longest-common-substring) with [composer](http://getcomposer.org/)using the following command:

```
composer require triun/longest-common-substring
```

Usage
=====

[](#usage)

Solver
------

[](#solver)

```
use Triun\LongestCommonSubstring\Solver;

$solver = new Solver();

$stringA = '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF';
$stringB = '56789AB56789ABCDE56789ABCDE56789AB56789A123456789A';

// calculates the LCSubstring to be '123456789A'
$result = $solver->solve($stringA, $stringB);
```

Matches solver
--------------

[](#matches-solver)

```
use Triun\LongestCommonSubstring\Solver;

$matchSolver = new MatchesSolver();

$stringA = '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF';
$stringB = '56789AB56789ABCDE56789ABCDE56789AB56789A123456789A';

$matches = $matchSolver->solve($stringA, $stringB);

// calculates the LCSubstring to be '123456789A'
$result = "$matches";
```

But also can give you the rest of the results which has the same length:

```
var_dump($matches->values());
```

```
array:12 [
  0 => "123456789A"
  1 => "56789ABCDE"
  2 => "56789ABCDE"
  3 => "123456789A"
  4 => "56789ABCDE"
  5 => "56789ABCDE"
  6 => "123456789A"
  7 => "56789ABCDE"
  8 => "56789ABCDE"
  9 => "123456789A"
  10 => "56789ABCDE"
  11 => "56789ABCDE"
]

```

You can use `unique` to skip duplicated values:

```
var_dump($matches->unique());
```

```
array:2 [
  0 => "123456789A"
  1 => "56789ABCDE"
]

```

Or even more information about the matches, like the input strings indexes:

```
var_dump($matches->values());
```

```
array:12 [
  0 => array:3 [
    "value" => "123456789A"
    "length" => 10
    "indexes" => array:2 [
      0 => 1
      1 => 40
    ]
  ]
  1 => array:3 [
    "value" => "56789ABCDE"
    "length" => 10
    "indexes" => array:2 [
      0 => 5
      1 => 7
    ]
  ]
  2 => array:3 [
    "value" => "56789ABCDE"
    "length" => 10
    "indexes" => array:2 [
      0 => 5
      1 => 17
    ]
  ]
  ...
]

```

Issues
======

[](#issues)

Bug reports and feature requests can be submitted on the [Github Issue Tracker](https://github.com/Triun/PHP-Longest-Common-Substring/issues).

Contributing
============

[](#contributing)

See [CONTRIBUTING.md](CONTRIBUTING.md) for information.

License
=======

[](#license)

This repository is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

2963d ago

### Community

Maintainers

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

---

Tags

lcslongest-common-substring

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/triun-longest-common-substring/health.svg)

```
[![Health](https://phpackages.com/badges/triun-longest-common-substring/health.svg)](https://phpackages.com/packages/triun-longest-common-substring)
```

###  Alternatives

[coolsam/nested-comments

Add Nested comments/replies to filament forms, infolists and resources

3110.0k](/packages/coolsam-nested-comments)

PHPackages © 2026

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