Longest common subsequence calculation in PHP with UTF-8 strings

I needed a PHP library that compares strings with a Longest common subsequence algorithm. Important was the support of UTF-8 or Multibyte Strings at all.

Install gordonlesti/longest-common-subsequence

You can find the small library at Github.


The library requires PHP 7.


The recommended installtion is via Composer.

composer require gordonlesti/longest-common-subsequence


Use GordonLesti\LongestCommonSubsequence\LongestCommonSubsequence.

use GordonLesti\LongestCommonSubsequence\LongestCommonSubsequence;

Calculate the length of the longest common subsequence of two strings.

$lcsDist = LongestCommonSubsequence::lcs("DABDC", "CBDBBAC");

UTF-8 support

The library supports UTF-8.

$lcsDist = LongestCommonSubsequence::lcs("ΛΕΡΒΗΔΟΗΜΑΕΞΞΘΒΗ", "ΗΝΞΛΒΑΚΛ");
Next Previous