1. interface StringSearchable  
  2. {  
  3.     public function search($substring$buffer);  
  4. }  
  5. class BoyerMooreStringSearch implements StringSearchable  
  6. {  
  7.     public $substring = null;  
  8.     public $buffer = '';  
  9.     public $jumpTable = array();  
  10.     protected $results = array();  
  11.  
  12.     public function __construct()  
  13.     {      
  14.           
  15.     }  
  16.     public function __destruct()  
  17.     {  
  18.     }  
  19.     public function search($substring$buffer)  
  20.     {      
  21.         $this->results = array();  
  22.         $this->substring = $substring;  
  23.         $this->buffer = $buffer;  
  24.         $this->deriveJumpTable();  
  25.           
  26.         $substringLen = strlen($this->substring);  
  27.         $currentCharIndex = $substringLen - 1;  
  28.         $bufferLen = strlen($this->buffer);  
  29.         while ($currentCharIndex < $bufferLen) {      
  30.             for ($i = $substringLen - 1; $i >= 0; $i--) {  
  31.                 if ($this->buffer[$currentCharIndex - $substringLen + $i + 1] == $this->substring[$i]) {  
  32.                     if ($i == 0) {  
  33.                         $this->results[] = $currentCharIndex - $substringLen;  
  34.                         $currentCharIndex += $this->getJumpLength($this->buffer[$currentCharIndex]);  
  35.                     } else {  
  36.                         continue;  
  37.                     }  
  38.                 } else {  
  39.                     $currentCharIndex += $this->getJumpLength($this->buffer[$currentCharIndex]);  
  40.                     break;  
  41.                 }  
  42.  
  43.             }  
  44.         }  
  45.         return (sizeof($this->results) > 0);  
  46.     }  
  47.       
  48.     protected function deriveJumpTable()  
  49.     {  
  50.         $maxJump = strlen($this->substring);  
  51.         for ($i = strlen($this->substring) - 2; $i >= 0; $i--) {  
  52.             if (!array_key_exists($this->substring[$i], $this->jumpTable)) {  
  53.                 $this->jumpTable[$this->substring[$i]]] = $maxJump - $i -1;  
  54.             }  
  55.         }  
  56.     }  
  57.     public function getJumpTable()  
  58.     {  
  59.         return $this->jumpTable;      
  60.     }  
  61.     public function getResults()  
  62.     {  
  63.         return $this->results;  
  64.     }  
  65.     public function getResultsCount()  
  66.     {  
  67.         return sizeof($this->results);  
  68.     }  
  69.     public function getJumpLength($charIndex)  
  70.     {  
  71.         if (array_key_exists($charIndex$this->jumpTable)) {  
  72.             return $this->jumpTable[$charIndex];  
  73.         } else {  
  74.             return strlen($this->substring);  
  75.         }  
  76.     }  
  77. }  
  78.  
  79. function Main()  
  80. {      
  81.     $poem = <<<POEM  
  82.     you son of bitch  
  83.     god damn it  
  84.     hey,god love me  
  85.     POEM;  
  86.     $bm = new BoyerMooreStringSearch();  
  87.     $bm->search('god'$poem);  
  88.     $count = $bm->getResultsCount;  
  89.     echo $count;