PHPackages                             dd/php-library-ddtextcompare - 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. dd/php-library-ddtextcompare

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

dd/php-library-ddtextcompare
============================

A simple tool allowing to compare two texts

v0.9(10y ago)13.0k↓50%MITPHPPHP &gt;=5.3.0

Since Nov 7Pushed 10y ago3 watchersCompare

[ Source](https://github.com/DivanDesign/php.library.ddTextCompare)[ Packagist](https://packagist.org/packages/dd/php-library-ddtextcompare)[ RSS](/packages/dd-php-library-ddtextcompare/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

ddTextCompare
=============

[](#ddtextcompare)

A simple tool allowing to compare two strings. The library works only with UTF-8 strings for now.

What this library is
--------------------

[](#what-this-library-is)

ddTextCompare can be used in situations when it is not possible to make the native string comparison in PHP. The library relies on chars number and their position, therefore, it's suitable for a comparison of strings containing typos or having different word order but the same words.

What this library isn't
-----------------------

[](#what-this-library-isnt)

ddTextCompare has nothing to do with morphological or any other analysis you would expect from an intelligent search engine. The main purpose for the library is to stay simple.

How it works
------------

[](#how-it-works)

By default ddTextCompare performs a simple analysis. Each string being compared is represented as two n-dimensional vectors, where n is the number of unique chars used in both strings. The firs vector v1 shows what chars are in its string and their total number. The other vector v2 shows where those unique chars are. When those two vectors are found for each string, cosine similarity will be found for the vector pairs. Once calculated, cos(v1,1v1,2) and cos(v2,1v2,2) are multiplied by their wights respectively and the result is modified to make it belong to a range between 0 and 1.

Installation
------------

[](#installation)

### Composer

[](#composer)

Just add the package to your composer.json.

```
composer require dd/php-library-ddtextcompare
```

### Manually

[](#manually)

Though it's convenient to use Composer, it is also possible to place the library wherever you want and include all the classes inside the “src” folder manually.

Basic usage
-----------

[](#basic-usage)

Here are some examples.

### Comparing strings with typos

[](#comparing-strings-with-typos)

```
$compare = new DDTextCompare();
$similarity = $compare->compare("Text without any typos", "Text wihtout ayn typoes");
// $similarity = 0.99076390557741
```

### Adjusting weights

[](#adjusting-weights)

By default, the weights for all criteria are equal, but it can be changed. When a wight is changed the another weight will be adjusted automatically to make their sum equal to 1.

```
$compare = new DDTextCompare();
$comparator = new Comparator\Cosine();

//Change the char total criterion weight
$comparator->setCharTotalWeight(0.8)

$similarity = $compare->compare("Text without any typos", "Txet wihtout ayn typoes", $comparator);
// $similarity = 0.99310343750374
```

### Extending

[](#extending)

A custom comparator class can be created by implementing the DDTextCompare\\Comparator interface.

```
$compare = new DDTextCompare();
$comparator = new Comparator\YourCustomComparator();

$similarity = $compare->compare("Text without any typos", "Txet wihtout ayn typoes", $comparator);
```

Changelog
---------

[](#changelog)

### Version 0.9 (2015-11-07)

[](#version-09-2015-11-07)

- + The first release.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

3845d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/09b81986267e62b5fde1473b40aa6f11f7bc26c1c29d5f80f2768c8788e08110?d=identicon)[dd](/maintainers/dd)

---

Top Contributors

[![psthmn](https://avatars.githubusercontent.com/u/1769215?v=4)](https://github.com/psthmn "psthmn (1 commits)")

---

Tags

ddtext comparestring comparedivan designddtextcompare

### Embed Badge

![Health badge](/badges/dd-php-library-ddtextcompare/health.svg)

```
[![Health](https://phpackages.com/badges/dd-php-library-ddtextcompare/health.svg)](https://phpackages.com/packages/dd-php-library-ddtextcompare)
```

PHPackages © 2026

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