<?php
// Truncate Strings Function - last updated 6.21.2010
//
// This function takes in the following (4) arguments:
// $string - the text you want to truncate
// $limit - after how many characters do you want to truncate
// $break - the character, you want have a break on
// (ie, break on a space, not in the middle of a word)
// $pad - chacters to add to the end
//
// This function returns a string, for saving/output/etc.
//
function truncate($string, $limit, $break=" ", $pad="...")
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break)))
{
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}
?>