Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created November 21, 2017 01:36
Show Gist options
  • Save lastday154/09bab8b9d0cfff370df939d0ae46f216 to your computer and use it in GitHub Desktop.
Save lastday154/09bab8b9d0cfff370df939d0ae46f216 to your computer and use it in GitHub Desktop.
Strstr PHP implementation
<?php
function search(string $pattern, string $text): int
{
$patternLength = strlen($pattern);
$textLength = strlen($text);
for ($i=0; $i< $textLength-$patternLength; $i++) {
for ($j=0; $j < $patternLength; $j++) {
if ($pattern[$j] != $text[$i+$j]) {
break;
}
}
if ($j == $patternLength) {
return $i;
}
}
return -1;
}
$text = "ACAADAABAAABAAAABA";
$pattern = "AABA";
echo search($pattern, $text);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment