KoderyBeta

Neat Trim

Category: PHP - Owner: PaulDavis - Owner ID: 1

Works like substr but lets the last word finish. Ends with '...'

Call like this; This pulls 200 characters, plus the last remaining characters of the last word.

// Trim string without cutting characters
function neatTrim($str, $n, $delim='...') { 
	$len = strlen($str); 
	if ($len > $n) { 
		preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); 
		return rtrim($matches[1]) . $delim; 
	} else { 
	return $str; 
	} 
}