Maybe this snippet i once wrote can be an assistance
it’s simply trim a text to a certain word length and add ellipses (...) if needed & desired (optional).
<?php
/*
* Trim a text to a certain word length and add ellipses (...) if needed & desired (optional).
* $str - is a string with the text to trim.
* $length - is an int length of how many words to use.
* $ellipses - 'yes' (optional), adds '...'.
*
* Stracture:
* [[!TrimWords? &str= &length= &ellipses=yes]]
*
* Usage Example:
* [[!TrimWords? &str='This is my string' &length=2 &ellipses=yes]] output will be: "This is...".
*/
$output = '';
if (isset($str, $length)) {
if (str_word_count($str,0) > $length) {
$output = implode(' ',array_slice(str_word_count($str,1),0,$length));
if ($ellipses == 'yes') $output .= '...';
}
else $output=$str;
}
return $output;
?>
Take in mind that it limits by the amount of words chosen over the length parameter.
if you want to restrict it by amount of chars it’s need to be modified,
over the sample below,
$length is an int of how many chars we want to limit.
it would end with the last whole word on the limited chars string.
<?php
$output = '';
if (isset($str, $length)) {
if (strlen($str) > $length) {
$output = substr($str, 0, strrpos(substr($str, 0, $length), ' '));
if ($ellipses == 'yes') $output .= '...';
}
else $output=$str;
}
return $output;
?>
you can also combine both to one snippet filter
another info that might help you:
http://rtfm.modx.com//display/revolution20/Input+and+Output+Filters
it’s a little bit strange that MODx doesn’t have it as a built-in filter, maybe they thought that the word warp & limit filters are enough.