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 | * @author Jordi Boggiano <j.boggiano@seld.be> |
---|
16 | * @copyright Copyright (c) 2008, Jordi Boggiano |
---|
17 | * @license http://dwoo.org/LICENSE Modified BSD License |
---|
18 | * @link http://dwoo.org/ |
---|
19 | * @version 1.1.0 |
---|
20 | * @date 2009-07-18 |
---|
21 | * @package Dwoo |
---|
22 | */ |
---|
23 | function Dwoo_Plugin_truncate(Dwoo_Core $dwoo, $value, $length=80, $etc='...', $break=false, $middle=false) |
---|
24 | { |
---|
25 | if ($length == 0) { |
---|
26 | return ''; |
---|
27 | } |
---|
28 | |
---|
29 | $value = (string) $value; |
---|
30 | $etc = (string) $etc; |
---|
31 | $length = (int) $length; |
---|
32 | |
---|
33 | if (strlen($value) < $length) { |
---|
34 | return $value; |
---|
35 | } |
---|
36 | |
---|
37 | $length = max($length - strlen($etc), 0); |
---|
38 | if ($break === false && $middle === false) { |
---|
39 | $value = preg_replace('#\s+(\S*)?$#', '', substr($value, 0, $length+1)); |
---|
40 | } |
---|
41 | if ($middle === false) { |
---|
42 | return substr($value, 0, $length) . $etc; |
---|
43 | } |
---|
44 | return substr($value, 0, ceil($length/2)) . $etc . substr($value, -floor($length/2)); |
---|
45 | } |
---|