Source for file modifier.truncate.php

Documentation is available at modifier.truncate.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8.  
  9. /**
  10.  * Smarty truncate modifier plugin
  11.  *
  12.  * Type:     modifier<br>
  13.  * Name:     truncate<br>
  14.  * Purpose:  Truncate a string to a certain length if necessary,
  15.  *           optionally splitting in the middle of a word, and
  16.  *           appending the $etc string or inserting $etc into the middle.
  17.  * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
  18.  *           truncate (Smarty online manual)
  19.  * @author   Monte Ohrt <monte at ohrt dot com>
  20.  * @param string 
  21.  * @param integer 
  22.  * @param string 
  23.  * @param boolean 
  24.  * @param boolean 
  25.  * @return string 
  26.  */
  27. function smarty_modifier_truncate($string$length 80$etc '...',
  28.                                   $break_words false$middle false)
  29. {
  30.     if ($length == 0)
  31.         return '';
  32.  
  33.     if (strlen($string$length{
  34.         $length -= min($lengthstrlen($etc));
  35.         if (!$break_words && !$middle{
  36.             $string preg_replace('/\s+?(\S+)?$/'''substr($string0$length+1));
  37.         }
  38.         if(!$middle{
  39.             return substr($string0$length$etc;
  40.         else {
  41.             return substr($string0$length/2$etc substr($string-$length/2);
  42.         }
  43.     else {
  44.         return $string;
  45.     }
  46. }
  47.  
  48. /* vim: set expandtab: */
  49.  
  50. ?>

Documentation generated on Sun, 09 Mar 2008 23:52:26 -0300 by phpDocumentor 1.4.0

SourceForge.net Logo Support This Project