|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* String Format |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Strings |
|
10
|
|
|
* @author Daniel Burke <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\strings; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* String Format |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Strings |
|
23
|
|
|
* @author Daniel Burke <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Format |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Shortens a string. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $string the input string |
|
35
|
|
|
* @param int $count the final length of the string |
|
36
|
|
|
* @param string $placeholder the placeholder string |
|
37
|
|
|
* |
|
38
|
|
|
* @return string the short string |
|
39
|
|
|
**/ |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public static function doStraightShorten($string, $count, $placeholder = '...') |
|
42
|
|
|
{ |
|
43
|
|
|
if (strlen($string.$placeholder) >= $count) { |
|
44
|
|
|
$string = substr($string, 0, $count) . $placeholder; |
|
45
|
|
|
} |
|
46
|
|
|
return $string; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Shortens a string. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $string the input string |
|
53
|
|
|
* @param int $count the final length of the string |
|
54
|
|
|
* @param string $placeholder the placeholder string |
|
55
|
|
|
* |
|
56
|
|
|
* @return string the short string without breaking a word |
|
57
|
|
|
**/ |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
public static function doShorten($string, $count, $placeholder = ' ...') |
|
60
|
|
|
{ |
|
61
|
|
|
if (strlen($string.$placeholder) >= $count) { |
|
62
|
|
|
$string = substr($string, 0, $count); |
|
63
|
|
|
$string = substr($string, 0, strrpos($string, ' ')) . $placeholder; |
|
64
|
|
|
} |
|
65
|
|
|
return $string; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|