PHP Classes

How to use a PHP fuzzy match string class to find similar strings in a text using the package Matchy: Perform exact or fuzzy searches in text strings

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-11-08 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
matchy 4.0.0Artistic License5PHP 5, Searching, Text processing, Ar...
Description 

Author

This package can perform exact or fuzzy searches in text strings.

It provides a class that can take a text string and returns the occurrences of another string in that string using exact or fuzzy matching.

The class can perform fuzzy search using different algorithms like:

- Finite State Automaton Matcher

- Rabin-Karp Matcher

- Knuth-Morris-Pratt Matcher

- Boyer-Moore Matcher

- Two-Way Matcher

The package also provides JavaScript and Python alternative implementations of the PHP class.

Innovation Award
PHP Programming Innovation award nominee
October 2025
Nominee
Vote
Fuzzy search is used to search for similar string in a longer text.

It is useful to search for text entered by users that may have made spelling mistakes.

This package implements a fuzzy search solution that supports many fuzzy search algorithms.

Manuel Lemos
Picture of Nikos M.
Name: Nikos M. is available for providing paid consulting. Contact Nikos M. .
Classes: 23 packages by
Country: Greece Greece
Age: 49
All time rank: 8449 in Greece Greece
Week rank: 195 Up2 in Greece Greece Up
Innovation award
Innovation award
Nominee: 10x

Winner: 2x

Instructions

Please check this example script to learn how to search for a similar in text.

Example

<?php
include(dirname(__FILE__) . '/../../src/php/Matchy.php');

function
create_string($alphabet, $n)
{
   
$s = '';
    for (
$i=0; $i<$n; ++$i)
    {
       
$s .= $alphabet[rand(0, count($alphabet)-1)];
    }
    return
$s;
}
function
create_pattern($string, $n)
{
   
$i = rand(0, strlen($string)-$n);
    return
substr($string, $i, $n);
}
function
test_case($matchy, $algorithm, $pattern, $string, $offset = 0)
{
   
$matcher = $matchy->{$algorithm}($pattern);
   
$found = $matcher($string, $offset);
   
$index = strpos($string, $pattern, $offset);
    if (
false === $index) $index = -1;
    echo(
$algorithm.'("'.$pattern.'", "'.$string.'", '.$offset.') = '.$found.($found === $index ? ' (true)' : ' (expected '.$index.')')."\n");
}
function
test()
{
   
$matchy = new Matchy();

   
$algorithms = [
   
'fsa',
   
'rabinkarp',
   
'knuthmorrispratt',
   
'twoway',
   
'boyermoore'
   
];

    for (
$i=0; $i<10; ++$i)
    {
       
$string = create_string(['a', 'b'/*, 'c', 'd'*/], 10);
       
$pattern = create_pattern($string, 5);

        echo(
"\n");
        foreach (
$algorithms as $algorithm)
        {
           
test_case($matchy, $algorithm, $pattern, $string);
        }
    }

   
/*
    // problematic
    echo("\n");
    echo('problematic'."\n");
    $problematic = [
    ['boyermoore', "bbbbb", "aabaabbbbb"],
    ['twoway', "babab", "aababababb"],
    ['twoway', "babab", "baaabababb"],
    ['boyermoore', "babab", "aababababb"],
    ['boyermoore', "abaaa", "baabaaabab"]
    ];
    foreach ($problematic as $entry) test_case($matchy, $entry[0], $entry[1], $entry[2]);
    */
}

test();


Details

Matchy

Exact and fuzzy string and regular expression matching algorithms for PHP, JavaScript, Python

Matchy

version: 4.0.0 almost complete

Included Algorithms:

A generic Non-Deterministic Finite Automaton is also included that can match exact regular expression patterns, fuzzy patterns, fuzzy regular expression patterns and various combinations of these.

The theory behind the implementation of the generic automaton is a custom fully functional and transparent fusion of a Non-Deterministic Finite Automaton for a given regular expression, and a Non-Deterministic Levenshtein Automaton for approximate matching either at char level or at word level (ie approximate words or approximate sentences) with option to take account of transpositions, ie a Damerau-Levenshtein Automaton.

examples:

see test/ folder

see also:

  • Abacus Computer Algebra and Symbolic Computation System for Combinatorics and Algebraic Number Theory for JavaScript and Python
  • TensorView view array data as multidimensional tensors of various shapes efficiently
  • Geometrize Computational Geometry and Rendering Library for JavaScript
  • Plot.js simple and small library which can plot graphs of functions and various simple charts and can render to Canvas, SVG and plain HTML
  • CanvasLite an html canvas implementation in pure JavaScript
  • Rasterizer stroke and fill lines, rectangles, curves and paths, without canvas
  • Gradient create linear, radial, conic and elliptic gradients and image patterns without canvas
  • css-color simple class to parse and manipulate colors in various formats
  • MOD3 3D Modifier Library in JavaScript
  • HAAR.js image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)
  • HAARPHP image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
  • FILTER.js video and image processing and computer vision Library in pure JavaScript (browser and node)
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python
  • GrammarTemplate grammar-based templating for PHP, JavaScript, Python
  • codemirror-grammar transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor
  • ace-grammar transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor
  • prism-grammar transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter
  • highlightjs-grammar transform a formal grammar in JSON format into a syntax-highlight mode for Highlight.js code highlighter
  • syntaxhighlighter-grammar transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter
  • Fuzzion a library of fuzzy / approximate string metrics for PHP, JavaScript, Python
  • Matchy a library of string matching algorithms for PHP, JavaScript, Python
  • PatternMatchingAlgorithms library of Pattern Matching Algorithms in JavaScript using Matchy
  • SortingAlgorithms library of Sorting Algorithms in JavaScript

Screenshots (1)  
  • matchy.jpg
  Files folder image Files (25)  
File Role Description
Files folder imagesrc (3 directories)
Files folder imagetest (3 directories)
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (25)  /  src  
File Role Description
Files folder imagejs (2 files)
Files folder imagephp (1 file)
Files folder imagepy (2 files)

  Files folder image Files (25)  /  src  /  js  
File Role Description
  Accessible without login Plain text file Matchy.js Data Auxiliary data
  Accessible without login Plain text file Matchy.min.js Data Auxiliary data

  Files folder image Files (25)  /  src  /  php  
File Role Description
  Plain text file Matchy.php Class Class source

  Files folder image Files (25)  /  src  /  py  
File Role Description
  Accessible without login Plain text file Matchy.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files (25)  /  test  
File Role Description
Files folder imagejs (6 files)
Files folder imagephp (6 files)
Files folder imagepy (6 files)

  Files folder image Files (25)  /  test  /  js  
File Role Description
  Accessible without login Plain text file out-fuzzy-nfa.txt Doc. Documentation
  Accessible without login Plain text file out-nfa.txt Doc. Documentation
  Accessible without login Plain text file out.txt Doc. Documentation
  Accessible without login Plain text file test-fuzzy-nfa.js Data Auxiliary data
  Accessible without login Plain text file test-nfa.js Data Auxiliary data
  Accessible without login Plain text file test.js Data Auxiliary data

  Files folder image Files (25)  /  test  /  php  
File Role Description
  Accessible without login Plain text file out-fuzzy-nfa.txt Doc. Documentation
  Accessible without login Plain text file out-nfa.txt Doc. Documentation
  Accessible without login Plain text file out.txt Doc. Documentation
  Accessible without login Plain text file test-fuzzy-nfa.php Example Example script
  Accessible without login Plain text file test-nfa.php Example Example script
  Accessible without login Plain text file test.php Example Example script

  Files folder image Files (25)  /  test  /  py  
File Role Description
  Accessible without login Plain text file out-fuzzy-nfa.txt Doc. Documentation
  Accessible without login Plain text file out-nfa.txt Doc. Documentation
  Accessible without login Plain text file out.txt Doc. Documentation
  Accessible without login Plain text file test-fuzzy-nfa.py Data Auxiliary data
  Accessible without login Plain text file test-nfa.py Data Auxiliary data
  Accessible without login Plain text file test.py Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0