Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty truncate modifier plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     modifier
Chris PeBenito 696b41
 * Name:     truncate
Chris PeBenito 696b41
 * Purpose:  Truncate a string to a certain length if necessary,
Chris PeBenito 696b41
 *           optionally splitting in the middle of a word, and
Chris PeBenito 696b41
 *           appending the $etc string.
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
Chris PeBenito 696b41
 *          truncate (Smarty online manual)
Chris PeBenito 696b41
 * @param string
Chris PeBenito 696b41
 * @param integer
Chris PeBenito 696b41
 * @param string
Chris PeBenito 696b41
 * @param boolean
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
Chris PeBenito 696b41
                                  $break_words = false)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    if ($length == 0)
Chris PeBenito 696b41
        return '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (strlen($string) > $length) {
Chris PeBenito 696b41
        $length -= strlen($etc);
Chris PeBenito 696b41
        if (!$break_words)
Chris PeBenito 696b41
            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
Chris PeBenito 696b41
      
Chris PeBenito 696b41
        return substr($string, 0, $length).$etc;
Chris PeBenito 696b41
    } else
Chris PeBenito 696b41
        return $string;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>