PHP Classes

Fuse: Fuzzy search of arrays using the Bitap algorithm

Recommend this page to a friend!
  Info   View files Documentation   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-04-12 (4 months ago) RSS 2.0 feedNot enough user ratingsTotal: 66 This week: 1All time: 8,885 This week: 558Up
Version License PHP version Categories
fuse 1.0Custom (specified...5Algorithms, PHP 5, Searching
Description Author

This package can perform fuzzy search of arrays using the Bitap algorithm.

It can take an array of data elements that contain arrays of property values.

It can index data elements by given keys so it can perform search for elements with property values with exact or similar text.

This package is a port of the fuse.js JavaScript library.

Innovation Award
PHP Programming Innovation award nominee
January 2017
Number 18
Fuzzy search allows determining if a text string is similar to another. Bitap is a fuzzy search algorithm.

This package provides a pure PHP implementation of the Bitap algorithm ti match strings in arrays.

Manuel Lemos
Name: AccountKiller <contact>
Classes: 1 package by
Country: ???
Age: ???
All time rank: 4097
Week rank: 458 Up
Innovation award
Innovation award
Nominee: 1x



Fuzzy search for PHP based on the Bitap algorithm

This is a PHP port of the awesome Fuse.js project and (as of their version 2.6.2) provides 100% API compatibility.

For an approximate demonstration of what this library can do, check out their demo & usage


This package is available via Composer. To add it to your project, just run:

composer require loilo/fuse


require_once 'vendor/autoload.php';

$fuse = new \Fuse\Fuse([
    "title" => "Old Man's War",
    "author" => "John Scalzi"
    "title" => "The Lock Artist",
    "author" => "Steve Hamilton"
    "title" => "HTML5",
    "author" => "Remy Sharp"
    "title" => "Right Ho Jeeves",
    "author" => "P.D Woodhouse"
], [
  "keys" => [ "title", "author" ],


  [0] => Array
      [title] => The Lock Artist
      [author] => Steve Hamilton
  [1] => Array
      [title] => HTML5
      [author] => Remy Sharp


keys (type: array)

List of properties that will be searched. This also supports nested properties:

$books = [
    "title" => "Old Man's War",
    "author" => [
      "firstName" => "John",
      "lastName" => "Scalzi"
$fuse = new \Fuse\Fuse($books, [
  "keys" => [ "title", "author.firstName" ]

id (type: string)

The name of the identifier property. If specified, the returned result will be a list of the items' identifiers, otherwise it will be a list of the items.

caseSensitive (type: bool, default: false)

Indicates whether comparisons should be case sensitive.

include (type: array, default: [])

An array of values that should be included from the searcher's output. When this array contains elements, each result in the list will be of the form [ "item" => ..., "include1" => ..., "include2" => ... ]. Values you can include are score, matches. For example:

[ "include" => [ "score", "matches" ] ]

shouldSort (type: bool, default: true)

Whether to sort the result list, by score.

searchFn (type: \Fuse\Searcher, default: \Fuse\BitapSearcher::class)

The search class to use. It is required to implement the \Fuse\Searcher interface.

getFn (type: function, default: \Fuse\Fuse::defaultValueGetter)

The get function to use when fetching an object's properties. The default will search nested paths like

@param $obj  The object or associative array being searched
@param $path The path to the target property

// example using an object with a `getter` method
'getFn' => function ($obj, $path) {
  return $obj->get($path);

sortFn (type: function, default: \Fuse\Fuse::defaultScoreSort)

The function that is used for sorting the result list.

location (type: int, default: 0)

Determines approximately where in the text is the pattern expected to be found.

threshold (type: float, default: 0.6)

At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything.

distance (type: int, default: 100)

Determines how close the match must be to the fuzzy location (specified by location). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match be at the exact location specified, a distance of 1000 would require a perfect match to be within 800 characters of the location to be found using a threshold of 0.8.

maxPatternLength (type: int, default: 32)

The maximum length of the search pattern. The longer the pattern, the more intensive the search operation will be. Whenever the pattern exceeds the maxPatternLength, an error will be thrown. Why is this important? Read this#Word_size_choice).

verbose (type: bool, default: false)

Will print out steps. Useful for debugging.

tokenize (type: bool, default: false)

When true, the search algorithm will search individual words and the full string, computing the final score as a function of both. Note that when tokenize is true, the threshold, distance, and location are inconsequential for individual tokens.

tokenSeparator (type: string, default: / +/g)

A regular expression string used to separate words of the search pattern when searching. Only applicable when tokenize is true.

matchAllTokens (type: bool, default: false)

When true, the result set will only include records that match all tokens. Will only work if tokenize is also true.

findAllMatches (type: bool, default: false)

When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string.

minMatchCharLength (type: int, default: 1)

When set to include matches, only those whose length exceeds this value will be returned. (For instance, if you want to ignore single character index returns, set to 2)



@param {string} $pattern The pattern string to fuzzy search on.
@return {array} A list of all search matches.

Searches for all the items whose keys (fuzzy) match the pattern.


@param {array} $list
@return {array} The newly set list

Sets a new list of data for Fuse to match against.

Weighted Search

In some cases you may want certain keys to be weighted differently:

$fuse = new \Fuse\Fuse($books, [
  "keys" => [
      "name" => "title",
      "weight" => 0.3
      "name" => "author",
      "weight" => 0.7

Where 0 < weight <= 1.


Before submitting a pull request, please add relevant unit tests to the test folder.

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files)
Files folder imagetest (19 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file BitapSearcher.php Class Class source
  Plain text file Fuse.php Class Class source
  Plain text file Searcher.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file books.php Aux. Auxiliary script
  Plain text file BookTest.php Class Class source
  Plain text file FruitTest.php Class Class source
  Plain text file IdNumberTest.php Class Class source
  Plain text file IncludeScoreTest.php Class Class source
  Plain text file LongPatternTest.php Class Class source
  Plain text file OptionsTest.php Class Class source
  Plain text file RecurseArraysTest.php Class Class source
  Plain text file RecurseIntoArrayObjectsTest.php Class Class source
  Plain text file SearchIdTest.php Class Class source
  Plain text file SearchListTest.php Class Class source
  Plain text file SearchLocationTest.php Class Class source
  Plain text file SearchNestedIdTest.php Class Class source
  Plain text file SetListTest.php Class Class source
  Plain text file TokenTest.php Class Class source
  Plain text file UseIdIncludeScoreTest.php Class Class source
  Plain text file UseIdTest.php Class Class source
  Accessible without login Plain text file users.php Aux. Auxiliary script
  Plain text file WeightedSearchTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:1
All time:8,885
This week:558Up
User Comments (1)
5 months ago (muabshir)