// Limit a string to a specified number of words and add '...' to end
FUNCTION neat_trim($str, $n, $delim='...') {
$len = STRLEN($str);
IF ($len > $n) {
PREG_MATCH('/(.{' . $n . '}.*?)\b/', $str, $matches);
RETURN RTRIM($matches[1]) . $delim;
}
ELSE {
RETURN $str;
}
}