Source for file truncate.php

Documentation is available at truncate.php

  1. <?php
  2.  
  3. /**
  4.  * Truncates a string at the given length
  5.  * <pre>
  6.  *  * value : text to truncate
  7.  *  * length : the maximum length for the string
  8.  *  * etc : the characters that are added to show that the string was cut off
  9.  *  * break : if true, the string will be cut off at the exact length, instead of cutting at the nearest space
  10.  *  * middle : if true, the string will contain the beginning and the end, and the extra characters will be removed from the middle
  11.  * </pre>
  12.  * This software is provided 'as-is', without any express or implied warranty.
  13.  * In no event will the authors be held liable for any damages arising from the use of this software.
  14.  *
  15.  * This file is released under the LGPL
  16.  * "GNU Lesser General Public License"
  17.  * More information can be found here:
  18.  * {@link http://www.gnu.org/copyleft/lesser.html}
  19.  *
  20.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  21.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  22.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  23.  * @link       http://dwoo.org/
  24.  * @version    0.9.1
  25.  * @date       2008-05-30
  26.  * @package    Dwoo
  27.  */
  28. function Dwoo_Plugin_truncate(Dwoo $dwoo$value$length=80$etc='...'$break=false$middle=false)
  29. {
  30.     if ($length == 0{
  31.         return '';
  32.     }
  33.  
  34.     $value = (string) $value;
  35.     $etc = (string) $etc;
  36.     $length = (int) $length;
  37.  
  38.     if (strlen($value$length{
  39.         return $value;
  40.     }
  41.  
  42.     $length max($length strlen($etc)0);
  43.     if ($break === false && $middle === false{
  44.         $value preg_replace('#\s*(\S*)?$#'''substr($value0$length+1));
  45.     }
  46.     if ($middle === false{
  47.         return substr($value0$length$etc;
  48.     }
  49.     return substr($value0ceil($length/2)) $etc substr($value-floor($length/2));
  50. }

Documentation generated on Sun, 07 Sep 2008 23:58:00 +0200 by phpDocumentor 1.4.0