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

/framework/vendor/smarty3/lib/libs/plugins/modifier.truncate.php

http://zoop.googlecode.com/
PHP | 64 lines | 37 code | 3 blank | 24 comment | 16 complexity | 490041fe1c6a0f9ff1dfd9a792d8f09f MD5 | raw file
 1<?php
 2/**
 3* Smarty plugin
 4* 
 5* @package Smarty
 6* @subpackage PluginsModifier
 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* 
18* @link http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual)
19* @author Monte Ohrt <monte at ohrt dot com> 
20* @param string $string input string
21* @param integer $length lenght of truncated text
22* @param string $etc end string
23* @param boolean $break_words truncate at word boundary
24* @param boolean $middle truncate in the middle of text
25* @return string truncated 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 (is_callable('mb_strlen')) {
34        if (mb_strlen($string) > $length) {
35            $length -= min($length, mb_strlen($etc));
36            if (!$break_words && !$middle) {
37                $string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
38            } 
39            if (!$middle) {
40                return mb_substr($string, 0, $length) . $etc;
41            } else {
42                return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
43            } 
44        } else {
45            return $string;
46        } 
47    } else {
48        if (strlen($string) > $length) {
49            $length -= min($length, strlen($etc));
50            if (!$break_words && !$middle) {
51                $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
52            } 
53            if (!$middle) {
54                return substr($string, 0, $length) . $etc;
55            } else {
56                return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
57            } 
58        } else {
59            return $string;
60        } 
61    } 
62} 
63
64?>