We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14147
    • 26 Posts
    No idea if anyone has done this before, but couldn’t find any reference when I searched the forums.

    When searching for text such as ’Rob’, the highlighting would only highlight the actual word, whereas I wanted it to highlight variations such as ’rob’ and ’ROB’ as well.  Inserting the code below at line 838 within the file /assets/snippets/ajaxSearch/classes/search.class.php does the trick. This is for version 1.8.0  of ajaxSearch.

    $finalExtract = preg_replace( '/' . preg_quote( strtoupper($searchTerm), '/' ) . '/' .$this->pcreModifier, '<span class="'.$highlightClass.' '.$highlightClass.$count.'">\0</span>', $finalExtract);
    $finalExtract = preg_replace( '/' . preg_quote( strtolower($searchTerm), '/' ) . '/' .$this->pcreModifier, '<span class="'.$highlightClass.' '.$highlightClass.$count.'">\0</span>', $finalExtract);
    $finalExtract = preg_replace( '/' . preg_quote( ucfirst($searchTerm), '/' ) . '/' .$this->pcreModifier, '<span class="'.$highlightClass.' '.$highlightClass.$count.'">\0</span>', $finalExtract);
    


    A nice little modification to the code, perhaps this could become a parameter in future.

    For those coding purists, the above was replaced with:

          // highligth the search terms if needed
          if ($this->cfg['highlightResult']){
            $count = 1;
            foreach($searchList as $searchTerm){
    		  $sArray = array();
    		  $sArray[] = '/' . preg_quote( $searchTerm, '/' ) . '/' .$this->pcreModifier;
    		  $sArray[] = '/' . preg_quote( strtoupper($searchTerm), '/' ) . '/' .$this->pcreModifier;
    		  $sArray[] = '/' . preg_quote( strtolower($searchTerm), '/' ) . '/' .$this->pcreModifier;
    		  $sArray[] = '/' . preg_quote( ucfirst($searchTerm), '/' ) . '/' .$this->pcreModifier;
    		  $rArray = array_fill(0, count($sArray), '<span class="'.$highlightClass.' '.$highlightClass.$count.'">\0</span>');
                      $finalExtract = preg_replace( $sArray, $rArray, $finalExtract);
                      $class .= ' '.$highlightClass.$count;
                      $count++;
            }
          }
    

      • 5811
      • 1,717 Posts
      Thanks a lot jigr69 smiley you solve - FS#45 - highlighting in extracts marks only the version typed in search form

      See http://modxcms.com/forums/index.php/topic,28845.msg175443.html#msg175443

      As I am a bit overbooked at this moment I hadn’t start an analyse of this issue before. Thanks, for the time saved and for the solution.
        • 5811
        • 1,717 Posts
        Hi Jigr69,

        I can’t reproduce this issue. Try for instance to search "School" or "SCHOOL" or "school" on the demo site here.

        Do you use the version 1.3 of the plugin advSearch Highlight or searchHighlight ? These new versions add:
        $pcreModifier = ($database_connection_charset == 'utf8') ? 'iu' : 'i';
        ...
        $pattern = '/' . preg_quote($word, '/') . '(?=[^>]*<)/' . $pcreModifier;
        ...
        $pattern = '/' . preg_quote($word, '/') . '(?=[^>]*<)/' . $pcreModifier;

        See : http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php regarding the PCRE_CASELESS modifier:

        i (PCRE_CASELESS)
        If this modifier is set, letters in the pattern match both upper and lower case letters.

        Thanks for your feedback to try to understand if it is really an issue.