Hi!
As I'm not good at PHP, I copied ContextSummary and added some improvements (?), since I had problems with encoded chars, stripped <br /> sticking words and results coming from blog entries including BBCode.
In Page class:
function MyContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true) {
if(!$string) $string = $_REQUEST['Search']; // Use the default "Search" request variable (from SearchForm)
/*** Prepare Content ***/
// Replace <br /> in order to get separate words
$content = str_replace('<br />', ' ', $this->Content);
// Decoding entities prevents XML validation error
$content = html_entity_decode($content, ENT_COMPAT , 'UTF-8');
// Remove HTML tags so we don't have to deal with matching tags
$text = strip_tags($content);
// Remove BBCode
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
$text = preg_replace($pattern, $replace, $text);
// Find the search string
$position = (int) stripos($text, $string);
// We want to search string to be in the middle of our block to give it some context
$position = max(0, $position - ($characters / 2));
if($position > 0) {
// We don't want to start mid-word
$position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n"));
}
$summary = substr($text, $position, $characters);
$stringPieces = explode(' ', $string);
if($highlight) {
// Add a span around all key words from the search term as well
if($stringPieces) foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
}
}
return trim($summary);
}
In Page_results.ss:
…
<% if Content %>
<p>…$MyContextSummary(375)…</p>
<% else %>
…
For what is worth,
Juan