| 93 |
- |
1 |
<?php namespace Fuse\Bitap;
|
|
|
2 |
|
|
|
3 |
function regex_search($text, $pattern, $tokenSeparator = ' +')
|
|
|
4 |
{
|
|
|
5 |
$regex = '/' . preg_replace('/' . str_replace('/', '\\/', $tokenSeparator) . '/', '|', str_replace('/', '\\/', preg_quote($pattern))) . '/';
|
|
|
6 |
|
|
|
7 |
$isMatch = (bool) preg_match($regex, $text, $matches);
|
|
|
8 |
$matchedIndices = [];
|
|
|
9 |
|
|
|
10 |
if ($isMatch) {
|
|
|
11 |
for ($i = 0, $matchesLen = sizeof($matches); $i < $matchesLen; $i++) {
|
|
|
12 |
$match = $matches[$i];
|
|
|
13 |
$matchedIndices[] = [ mb_strpos($text, $match), mb_strlen($match) - 1 ];
|
|
|
14 |
}
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
return [
|
|
|
18 |
'score' => $isMatch ? 0.5 : 1,
|
|
|
19 |
'isMatch' => $isMatch,
|
|
|
20 |
'matchedIndices' => $matchedIndices
|
|
|
21 |
];
|
|
|
22 |
}
|