PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/smarty/plugins/modifier.truncate.php

http://prosporous.googlecode.com/
PHP | 50 lines | 21 code | 5 blank | 24 comment | 8 complexity | da35c3d0a2dfc9ea11c32b3a10246aa7 MD5 | raw file
 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 */
27function 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($length, strlen($etc));
35        if (!$break_words && !$middle) {
36            $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
37        }
38        if(!$middle) {
39            return substr($string, 0, $length) . $etc;
40        } else {
41            return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
42        }
43    } else {
44        return $string;
45    }
46}
47
48/* vim: set expandtab: */
49
50?>