Shorten string

17 Ⅴ 2010

This is a small function that shortens a string.
It looks if the string is longer than a preset length, and if so cuts the text there.
Then it removes the last word from the string (so your string ends neatly at a space), and adds three dots.

// Shorten a text if nessecary
function shortenText($txt='', $len=0){

	if(strlen($txt) > $len){
		$txt = substr($txt, 0, $len);
		$t = split("[\n\r\t ]+", $txt);
		$t = array_slice($t, 0, -1);
		$txt = join(' ', $t);
		$txt .= '...';

	return $txt;