Created
December 4, 2017 17:19
-
-
Save pedroborges/c0fafc7ff2bcff6d485263ff74145bc4 to your computer and use it in GitHub Desktop.
Port of https://github.com/bevacqua/fuzzysearch to PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function characterCodeAt(string $string, int $index) { | |
return ord(substr($string, $index, 1)); | |
} | |
function fuzzySearch(string $needle, string $haystack) { | |
$needleLength = strlen($needle); | |
$haystackLength = strlen($haystack); | |
if ($needleLength > $haystackLength) { | |
return $needle === $haystack; | |
} | |
for ($i = 0, $j = 0; $i < $needleLength; $i++) { | |
$needleCharacter = characterCodeAt($needle, $i); | |
while ($j < $haystackLength) { | |
if (characterCodeAt($haystack, $j++) === $needleCharacter) { | |
// Go back to the for loop | |
continue 2; | |
} | |
} | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment