title: Google like search results
body: <p>Leading on from my last question:</p>
  
  <p><a href="https://stackoverflow.com/questions/7944451/tidying-search-results-from-database-php">Tidying search results from database php</a></p>
  
  <p>My research has led me to the following code to truncate search results, highlight the keyword and display a number of characters to the left and right of the keyword. Looking better.</p>
  
  <p>My question now is, with the below code, by adding characters to the left or right of the keyword it is cutting words in half. </p>
  
  <p><strong>example:</strong></p>
  
  <pre><code>Savings plans
  
  ..le="background: #E13300;"&gt;investing in international accounts is no longer the premise of the rich and famous, all expatriates living abroad can now enjoy flex...
  </code></pre>
  
  <p>How can I make the truncate function stop cutting my words and tags in half????</p>
  
  <p>Code:</p>
  
  <pre><code> $word = 'invest'; 
  $characters_before="80";
  $characters_after="80";
  function supertruncate($text, $word, $characters_before, $characters_after){
                    $pos = strpos($text, $word);
      $start = $characters_before &lt; $pos ? $pos - $characters_before : 0;
      $len = $pos + strlen($word) + $characters_after - $start;
                     $text = str_ireplace($word, '&lt;span class="highlight" style="background: #E13300;"&gt;' . $word . '&lt;/span&gt;', $text);
      return substr($text, $start, $len);
  }
  </code></pre>