File: src/Bitap/regex_search.php

Recommend this page to a friend!
  Classes of AccountKiller  >  Fuse  >  src/Bitap/regex_search.php  >  Download  
File: src/Bitap/regex_search.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Fuse
Fuzzy search of arrays using the Bitap algorithm
Author: By
Last change: Escape slashes in RegEx search patterns, fixes #5
Date: 2 years ago
Size: 698 bytes
 

Contents

Class file image Download
<?php namespace Fuse\Bitap;

function
regex_search($text, $pattern, $tokenSeparator = ' +')
{
   
$regex = '/' . preg_replace('/' . str_replace('/', '\\/', $tokenSeparator) . '/', '|', str_replace('/', '\\/', preg_quote($pattern))) . '/';

   
$isMatch = (bool) preg_match($regex, $text, $matches);
   
$matchedIndices = [];

    if (
$isMatch) {
        for (
$i = 0, $matchesLen = sizeof($matches); $i < $matchesLen; $i++) {
           
$match = $matches[$i];
           
$matchedIndices[] = [ mb_strpos($text, $match), mb_strlen($match) - 1 ];
        }
    }

    return [
       
'score' => $isMatch ? 0.5 : 1,
       
'isMatch' => $isMatch,
       
'matchedIndices' => $matchedIndices
   
];
}


For more information send a message to info at phpclasses dot org.