<?php

function LimitString ($TheString, $MaxChars, $Threshold = 5)
{
	if (strlen ($TheString) <= $MaxChars)
                return $TheString;

        // (1) hard upper limit is $MaxChars
        $Work = substr ($TheString, 0, $MaxChars);

	// (2) try to find a word boundary at the end to make it look nicer
	$Pos = strrpos ($Work, ' ');
	if ($Pos >= ($MaxChars - $Threshold))
		$Work = substr ($Work, 0, $Pos);

	$Work .= ' ...';

        return $Work;
}


?>