We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19522
    • 17 Posts
    The default snippet code is doing some redundant work in the part that constructs the highligted summary when handling more than one search term. Also it works fine without a special case when there is only one search term as foreach works on an array with only one element as well.
    Here is a fix that optimizes that part considerably when searching for more than one word. On busy sites with large pages it could have a huge effect.

    Line 511 of snippet-ajaxSearch-tpl.php

    Replace
    [tt]
    if ($extract) {
    $highlightClass = ’ajaxSearch_highlight’;
    $text=$SearchFormsrc[’content’];
    if (count($search)>1){
    $count=1;
    $summary=’’;
    foreach ($search as $searchTerm){
    $summary .= PrepareSearchContent( $text, $length=200, $searchTerm );
    $summary = preg_replace( ’/’ . preg_quote( $searchTerm, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight’.$count.’">\0</span>’, $summary );
    $highlightClass .= ’ ajaxSearch_highlight’.$count;
    $count++;
    }
    $text=$summary;
    } else {
    $search=$searchString;
    $text=PrepareSearchContent( $text, $length=200, $search );
    $text = preg_replace( ’/’ . preg_quote( $searchString, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight1">\0</span>’, $text );
    $highlightClass .= ’ ajaxSearch_highlight1’;
    }
    }
    [/tt]
    with
    [tt]
    if ($extract) {
    $highlightClass = ’ajaxSearch_highlight’;
    $text=$SearchFormsrc[’content’];
    $count=1;
    $summary=’’;
    $toAdd = PrepareSearchContent( $text, $length=200, $search[0] );
    strip_tags( $text );
    foreach ($search as $searchTerm) {
    if (preg_match(’/’ . preg_quote($searchTerm) . ’/i’, $text)) {
    if ($count > 1) { // The first summary was already extracted above
    $toAdd = SmartSubstr( $text , $length=200, $searchTerm );
    }
    $summary .= preg_replace( ’/’ . preg_quote( $searchTerm, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight’.$count.’">\0</span>’, $toAdd ) . ’ ’;
    }
    $highlightClass .= ’ ajaxSearch_highlight’.$count;
    $count++;
    }
    $text=$summary;
    }
    [/tt]
      • 12652
      • 228 Posts
      Quote from: Pete at Dec 21, 2006, 02:50 AM

      ... If you view the source of your content which is supposed to be highlighted, does it have all the styles in place around the words that are supposed to be highlighted? ...

      No, so it seems there must be something interfering with the processing for the highlighting. Can’t focus too much on it at the moment, but may revisit and try to some trial and error to see if something is conflicting.
        | Identity Developments delivers SEO focused web design and web presence services
        - it&#39;s not about websites, it&#39;s about your identity. |
        • 19522
        • 17 Posts
        The highlighting on the final page when following a link from the summary page was terribly broken for me as well, only highlighting (n+1)*n/2 words on the entire page when using n search terms.
        Here is a fix for that. My regex still needs a little work to be fail safe, though. I’ll post an updated one later.

        in Search_Highlight_plugin.php line 74 replace
        [tt]
        $pattern = ’(>[^<]*)(’. quotemeta($word) .’)’;
        $replacement = ’\\1<span class="’.$class.’">\\2</span>’;
        $body[1] = eregi_replace($pattern, $replacement, $body[1]);
        [/tt]
        with
        [tt]
        $quotedWord = preg_quote($word);
        $pattern = ’/’ . $quotedWord . ’/i’;
        $replacement = ’<span class="’.$class.’">’ . $quotedWord . ’</span>’;
        $body[1] = preg_replace($pattern, $replacement, $body[1]);
        [/tt]

        *For plugin and snippet changes on an already installed system altering just the files won’t work. You
        need to locate the resource in the manager and alter it there for the database to be updated.*
        ** PLEASE DON’T USE AS IS ON PRODUCTION SITES as for now it will break the output on the final page if searching for text that also occurs inside tags like ’body’. **
        *** See updated fix below which is much more solid. The warning above does not apply to that***
          • 15987
          • 786 Posts
          Wow, I am amazed at the additions while I was gone. I will give these all a good look over and try to get them all integrated into a new realease. Thanks for all of the great additions smiley
            • 12652
            • 228 Posts
            Hope you had a good time on the slopes. Obviously a well earned break!

            As they say, all work and no play makes you... me, actually, come to think of it sad

            cheers
              | Identity Developments delivers SEO focused web design and web presence services
              - it&#39;s not about websites, it&#39;s about your identity. |
              • 19522
              • 17 Posts
              I’ve come up with something that seems to be working correctly. I was having some problems because PHP does not yet support variable length look around in regex patterns.

              in Search_Highlight_plugin.php line 74 use this replacement code instead:
              [tt]
              $pattern = ’/(\>[^\<]+)(’ . preg_quote($word) . ’)/iU’;
              $replacement = ’${1}<span class="’ . $class . ’">${2}</span>’;
              while (($tmp = preg_replace($pattern, $replacement, $body[1])) != $body[1])
              $body[1] = $tmp;
              [/tt]
              *** Edit: See an even better solution below ***
                • 5338
                • 35 Posts
                I look forward to see all the modification in one integrated version.

                In the mean time I have a problem with charset. I’m using utf-8 to display accented characters (Italian). It works well everywhere except in the ajax results preview (hover-) pane. In that div, it shows accented letters with a strange sequence, like if it was not set charset="utf-8" in the head of html page (<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />)...

                Anyone knows why it happens?

                BTW, merry christmas to everyone!!!
                  • 19522
                  • 17 Posts
                  This is even better:
                  Line 74 of Search_Highlight_plugin.php, use this replacement instead of either of the two suggested above.
                  [tt]
                  $pattern = ’/’ . preg_quote($word) . ’(?=[^>]*<)/i’;
                  $replacement = ’<span class="’ . $class . ’">${0}</span>’;
                  $body[1] = preg_replace($pattern, $replacement, $body[1]);
                  [/tt]

                  cino, I’m seeing strange charecters instead of non-ascii characters on the results summary page as well, but only sometimes. I haven’t found the culprit yet (haven’t looked much either). Will see if I get the time to locate it.
                    • 19522
                    • 17 Posts
                    The extra trailing space at the results summary page, ’results found for "foo bar baz "’ was annoying me so I added a line to remove that extra space at the end.
                    line 499 of snippet-ajaxSearch-tpl.php

                    Aftter
                    [tt]
                    $hits++;
                    }
                    [/tt]
                    I inserted this code:
                    [tt]
                    // Remove trailing ’&nbsp;’
                    $searchwords = substr($searchwords, 0, strlen($searchwords) -6);
                    [/tt]
                      • 5338
                      • 35 Posts
                      I found a solution for my problem. It’s because I set the page as utf-8. if you don’t have this setting you don’t have the problem (I think)

                      Anyway my solution (this is only a patch) is to change line 86 of AjaxSearch.php file
                      from:
                                  $result .='  <a class="AS_ajax_resultLink" href="index.php?id='.strval($row['id']).'&searched='.urlencode($searchString).'&highlight='.urlencode($highlightClass).'" title="' . $row['longtitle'] . '">' . $row['pagetitle'] . "</a>";
                      

                      to:
                                  $result .='  <a class="AS_ajax_resultLink" href="index.php?id='.strval($row['id']).'&searched='.urlencode($searchString).'&highlight='.urlencode($highlightClass).'" title="' . $row['longtitle'] . '">' . mb_convert_encoding($row['pagetitle'], "auto", "UTF-8") . "</a>";
                      


                      Another issue I had was that if someone make a search with an apostrophe character (’) it generates an error. I corrected this adding the following line just after line 46 always of AjaxSearch.php file:
                      $searchString = str_replace("'", "\\'", $searchString);
                      

                      This discussion is closed to further replies. Keep calm and carry on.