If you run a community driven website and cannot control the text submitted by users, this function may be of use. Sometimes, submitted text is just too long to fit in a specific area, but too important to not exist at all. This function will allow you to quickly truncate the string, and add a trailing string to the truncated one.

For example:Edit

Original: "The quick brown fox jumps over the lazy dog."

Truncated: "The quick brown fox jumps over..."

The Code:Edit

function truncate($string, $length, $trailing) {
if(strlen($string) > $length) return substr($string,0,$length).$trailing;
return $string;