Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function badCharHeuristic($str, $size, &$badchar)
- {
- for ($i = 0; $i < 256; $i++)
- $badchar[$i] = -1;
- for ($i = 0; $i < $size; $i++)
- $badchar[ord($str[$i])] = $i;
- }
- function SearchString($str, $pat) {
- $m = strlen($pat);
- $n = strlen($str);
- $i = 0;
- badCharHeuristic($pat, $m, $badchar);
- $s = 0;
- while ($s <= ($n - $m))
- {
- $j = $m - 1;
- while ($j >= 0 && $pat[$j] == $str[$s + $j])
- $j--;
- if ($j < 0)
- {
- $arr[$i++] = $s;
- $s += ($s + $m < $n) ? $m - $badchar[ord($str[$s + $m])] : 1;
- }
- else
- $s += max(1, $j - $badchar[ord($str[$s + $j])]);
- }
- for ($j = 0; $j < $i; $j++)
- {
- $result[$j] = $arr[$j];
- }
- return $result;
- }
- $text = "shit";
- echo "<b> Got text! </b><br>";
- $data = file_get_contents("english.txt");
- echo "<b> Done reading file! </b><br>";
- $time_start = microtime(true);
- $value = SearchString($data, $text);
- $time_end = microtime(true);
- $exec_time = ($time_end - $time_start);
- echo "<b> Time: </b>", $exec_time, "ms<br>";
- echo "Searched string! <br>";
- $cock = implode(", ", $value);
- echo "The string: ", "<b><u>", $text, "</u></b>", " was found in: ", $cock;
- $data = null;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement