Advertisement
Guest User

Untitled

a guest
May 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2.  
  3. function badCharHeuristic($str, $size, &$badchar)
  4. {
  5.     for ($i = 0; $i < 256; $i++)
  6.         $badchar[$i] = -1;
  7.  
  8.     for ($i = 0; $i < $size; $i++)
  9.         $badchar[ord($str[$i])] = $i;
  10. }
  11.  
  12. function SearchString($str, $pat) {
  13.     $m = strlen($pat);
  14.     $n = strlen($str);
  15.     $i = 0;
  16.  
  17.     badCharHeuristic($pat, $m, $badchar);
  18.  
  19.     $s = 0;
  20.     while ($s <= ($n - $m))
  21.     {
  22.         $j = $m - 1;
  23.  
  24.         while ($j >= 0 && $pat[$j] == $str[$s + $j])
  25.             $j--;
  26.  
  27.         if ($j < 0)
  28.         {
  29.             $arr[$i++] = $s;
  30.             $s += ($s + $m < $n) ? $m - $badchar[ord($str[$s + $m])] : 1;
  31.         }
  32.         else
  33.             $s += max(1, $j - $badchar[ord($str[$s + $j])]);
  34.     }
  35.  
  36.     for ($j = 0; $j < $i; $j++)
  37.     {
  38.         $result[$j] = $arr[$j];
  39.     }
  40.  
  41.     return $result;
  42. }
  43. $text = "shit";
  44. echo "<b> Got text! </b><br>";
  45. $data = file_get_contents("english.txt");
  46. echo "<b> Done reading file! </b><br>";
  47. $time_start = microtime(true);
  48. $value = SearchString($data, $text);
  49. $time_end = microtime(true);
  50. $exec_time = ($time_end - $time_start);
  51. echo "<b> Time: </b>", $exec_time, "ms<br>";
  52. echo "Searched string! <br>";
  53. $cock = implode(", ", $value);
  54. echo "The string: ", "<b><u>", $text, "</u></b>", " was found in: ", $cock;
  55. $data = null;
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement