Merged revisions 46568 via svnmerge from

svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-privacy

........
  r46568 | aoneil | 2007-12-11 10:20:00 +1300 (Tue, 11 Dec 2007) | 1 line
  
  Added a ContextSummary method, which returns a block of text surrounding a word
........


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@46779 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew O'Neil 2007-12-13 21:50:00 +00:00
parent 777c35c5d6
commit bb5f6593e8

View File

@ -212,6 +212,31 @@ class Text extends DBField {
} }
} }
function ContextSummary($characters = 500, $string = false, $html = true) {
if(!$string) {
// If no string is supplied, use the string from a SearchForm
$string = $_REQUEST['Search'];
}
// Remove HTML tags so we don't have to deal with matching tags
$text = $html ? $this->NoHTML() : $this->value;
// 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 = (int) strrpos($text, ' ', -$position);
}
$summary = substr($text, $position, $characters);
return $summary;
}
/** /**
* Allows a sub-class of TextParser to be rendered. @see TextParser for implementation details. * Allows a sub-class of TextParser to be rendered. @see TextParser for implementation details.
*/ */