Making search results break on word boundaries

Permalink
If you're looking to have your search results not show partial words, here is code that may work for you. The default Concrete5 search can cut-off words in the middle.

Create /blocks/search/controller.php and use this code:
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class SearchBlockController extends Concrete5_Controller_Block_Search {
   public function highlightedExtendedMarkup($fulltext, $highlight) {
      $text = @preg_replace("#\n|\r#", ' ', $fulltext);
      $matches = array();
      $highlight = str_replace(array('"',"'","""),'',$highlight); // strip the quotes as they mess the regex
      if (!$highlight) {
         $text = Loader::helper('text')->shorten($fulltext, 180);
         if (strlen($fulltext) > 180) {
            $text . '…<wbr>';
         }
         return $text;
      }
      // added white space character at beginning and end so words aren't cut off


Note that there is only one change from the original highlightedExtendedMarkup function: the $regex pattern (adding a whitespace character to the front and back, and lengthened the number of characters to compensate for the whitespace requirement).

I also added spaces around the ellipses for preference.

Hope that helps someone!

*** EDIT ***
I updated what characters will match, adding comma, colon, semi-colon and dashes. It probably could be made to any character ([.]) if desired.

kirkroberts