parents-merge 41017 - Added Text::LimitSentences

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@45060 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2007-11-19 02:03:30 +00:00
parent 528fb572fe
commit 202b7fd9b6

View File

@ -57,6 +57,34 @@ class Text extends DBField {
return $ret;
}
/**
* Limit sentences, can be controlled by passing an integer.
* @param int $sentCount The amount of sentences you want.
*/
function LimitSentences($sentCount = 2) {
$data = Convert::xml2raw($this->value);
$sentences = explode('.', $data);
if(count($sentences) == 1) {
return $sentences[0] . '.';
} elseif(count($sentences) > 1) {
if(is_numeric($sentCount) && $sentCount != 0) {
if($sentCount == 1) {
$output = $sentences[0] . '. ';
} else {
for($i = 1; $i <= $sentCount-1; $i++) {
if($sentences[0]) {
$output .= $sentences[0] . '. ';
}
if($sentences[$i]) {
$output .= $sentences[$i] . '. ';
}
}
}
return $output;
}
}
}
/**
* Caution: Not XML/HTML-safe - does not respect closing tags.