"; # show a snippet from the top of the document $j = index( $pageText, " ", $snippetlen ) ; # end on word boundary $t = substr($pageText, 0, $j); $t =~ s/($searchstring)/\1<\/SPAN>/gi ; $output .= $t . " ... " ; $pageText = substr( $pageText, $j ) ; # to avoid rematching # search for occurrences of searchstring $jsnippet = 0 ; while ( $jsnippet < $maxsnippets && $pageText =~ m/($searchstring)/i ) { # captures match as $1 $jsnippet++ ; # paranoid about looping if ( ($j = index( $pageText, $1 )) > -1 ) { # get index of match # get substr containing (start of) match, ending on word boundaries $start = index( $pageText, " ", $j-($snippetlen/2) ) ; $start = 0 if ( $start == -1 ) ; $end = index( $pageText, " ", $j+($snippetlen/2) ) ; $end = length( $pageText ) if ( $end == -1 ) ; $t = substr( $pageText, $start, $end-$start ) ; # highlight occurrences and tack on to output stream. $t =~ s/($searchstring)/\1<\/SPAN>/gi ; $output .= $t . " ... " ; # truncate text to avoid rematching the same string. $pageText = substr( $pageText, $end ) ; } } $output .= "