1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Common\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Manipulador de Strings |
7
|
|
|
*/ |
8
|
|
|
class Str |
9
|
|
|
{ |
10
|
|
|
const TRUNCATE_BEFORE = 0; |
11
|
|
|
const TRUNCATE_AFTER = 1; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $string |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public static function toUrl($string) |
18
|
|
|
{ |
19
|
|
|
$url = iconv('UTF-8', 'ASCII//TRANSLIT', $string); |
20
|
|
|
$url = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $url); |
21
|
|
|
$url = preg_replace("/[\/_| -]+/", '-', $url); |
22
|
|
|
$url = strtolower(trim($url, '-')); |
23
|
|
|
|
24
|
|
|
return $url; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $string |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function toFileName($string) |
32
|
|
|
{ |
33
|
|
|
return static::toUrl($string); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $string |
38
|
|
|
* @return int |
39
|
|
|
*/ |
40
|
|
|
public static function length($string) |
41
|
|
|
{ |
42
|
|
|
return mb_strlen($string); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $string |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function lower($string) |
50
|
|
|
{ |
51
|
|
|
return mb_strtolower($string); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $string |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public static function upper($string) |
59
|
|
|
{ |
60
|
|
|
return mb_strtoupper($string); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $string |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function lowerCamel($string) |
68
|
|
|
{ |
69
|
|
|
preg_match('/^_*/', $string, $begin); |
70
|
|
|
|
71
|
|
|
return $begin[0] . lcfirst(static::camel($string)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $string |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public static function camel($string) |
79
|
|
|
{ |
80
|
|
|
$string = ucwords(strtolower(trim(str_replace(['-', '_'], ' ', $string)))); |
81
|
|
|
|
82
|
|
|
return preg_replace('/[^a-zA-Z0-9]/', '', $string); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retorna a string resumida, sem cortar a última palavra |
87
|
|
|
* @param string $string |
88
|
|
|
* @param int $limit tamanho máximo |
89
|
|
|
* @param int $mode TRUNCATE_BEFORE | TRUNCATE_AFTER |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public static function truncate($string, $limit, $mode = self::TRUNCATE_BEFORE) |
93
|
|
|
{ |
94
|
|
|
if (mb_strlen($string) > $limit) { |
95
|
|
|
$string = strip_tags($string); |
96
|
|
|
$limit = static::calcLimit($string, $limit, $mode); |
97
|
|
|
$string = rtrim(mb_substr($string, 0, $limit), ' ,.!?') . '...'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $string; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Calcula o limite ideal |
105
|
|
|
* @param string $string |
106
|
|
|
* @param int $limit |
107
|
|
|
* @param int $mode |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
protected static function calcLimit($string, $limit, $mode) |
111
|
|
|
{ |
112
|
|
|
if ($mode === static::TRUNCATE_BEFORE) { |
113
|
|
|
$limit = mb_strrpos(mb_substr($string, 0, $limit + 1), ' '); |
114
|
|
|
} elseif ($mode === static::TRUNCATE_AFTER) { |
115
|
|
|
$limit = mb_strpos(mb_substr($string, $limit), ' ') + $limit; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $limit; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Limpa a string, retirando espaços e tags html |
123
|
|
|
* @param string $string |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public static function strip($string) |
127
|
|
|
{ |
128
|
|
|
return trim(strip_tags($string)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|