Skip to content

Instantly share code, notes, and snippets.

@pedroborges
Created December 4, 2017 17:19
Show Gist options
  • Save pedroborges/c0fafc7ff2bcff6d485263ff74145bc4 to your computer and use it in GitHub Desktop.
Save pedroborges/c0fafc7ff2bcff6d485263ff74145bc4 to your computer and use it in GitHub Desktop.
<?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