title: Highlight function highlights words in words
body: <p>i've developed the following function to highlight search results names.</p>
  
  <pre><code>function highlight($text, $words)
      {
          if (!is_array($words))
          {
              $words = preg_split('#\\W+#', $words, -1, PREG_SPLIT_NO_EMPTY);
          }
  
          $regex = '#\\b(\\w*)(';
          $sep = '';
          foreach ($words as $word)
          {
              $regex .= $sep . preg_quote($word, '#');
              $sep = '|';
          }
          $regex .= ')(\\w*)\\b#i';
  
          $text = preg_replace($regex, '\\1&lt;b&gt;\\2&lt;/b&gt;\\3', $text);
          $text = str_replace("&lt;/b&gt; &lt;b&gt;"," ",$text);
          return $text;
      }
  </code></pre>
  
  <p>My problem is that if the text is "This is the end" 
  and i'm looking for "this is" it will highlight like that: "th<strong>is is</strong> the end" because the word "is" is in "this", too.</p>
  
  <p>Somebody know how to fix that?</p>
  
  <p>My first thought was to only replace whitespace[whitespace] but that's not a good solution because the first word in a sentence got no whitespace at the beginning. :-(</p>
  
  <p>Thank you for your help.</p>