1 | <?php |
||
16 | class Str |
||
17 | { |
||
18 | /** |
||
19 | * CamelCases words or a dash name (eg. "hey-you" or "hey you" becomes "heyYou"). |
||
20 | * NB! This method is only for property formatted separated none |
||
21 | * multibyte (UTF-8) ASCII strings. It doesn't handle malformed strings with |
||
22 | * a lot of other weird characters. |
||
23 | * |
||
24 | * @param string $separatorString |
||
25 | * @param string $separator |
||
26 | * @param bool $upperCamelCase If first character should be capitalized (eg. "HeyYou" instead of "heyYou") |
||
27 | * @return string |
||
28 | */ |
||
29 | 2 | public static function separatorToCamel($separatorString, $separator = '_', $upperCamelCase = false) |
|
38 | |||
39 | /** |
||
40 | * Converts CamelCase text to lowercase separator separated text (eg. "heyYou" becomes "hey-you"). |
||
41 | * NB! This method is only for none multibyte (UTF-8) ASCII strings. |
||
42 | * |
||
43 | * @param string $camelCaseString |
||
44 | * @param string $separator |
||
45 | * @return string |
||
46 | */ |
||
47 | 1 | public static function camelToSeparator($camelCaseString, $separator = '_') |
|
54 | |||
55 | /** |
||
56 | * Get a random string generated from provided values. |
||
57 | * |
||
58 | * @param int $charCount |
||
59 | * @param string $characters |
||
60 | * @return string |
||
61 | */ |
||
62 | 1 | public static function random($charCount, $characters = 'abcdefghijklmnopqrstuvqxyz0123456789') |
|
63 | { |
||
64 | 1 | $randomString = ''; |
|
65 | 1 | for ($i = 0; $i < $charCount; $i++) { |
|
66 | 1 | $pos = mt_rand(0, strlen($characters) - 1); |
|
67 | 1 | $randomString .= $characters[$pos]; |
|
68 | 1 | } |
|
69 | |||
70 | 1 | return $randomString; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get shortened string. |
||
75 | * |
||
76 | * @param bool $string |
||
77 | * @param int $maxLength |
||
78 | * @param string $indicator |
||
79 | * @return string |
||
80 | */ |
||
81 | 1 | public static function truncate($string, $maxLength, $indicator = '...') |
|
82 | { |
||
83 | 1 | if (mb_strlen($string) > $maxLength) { |
|
84 | 1 | $string = mb_substr($string, 0, $maxLength - mb_strlen($indicator)) . $indicator; |
|
85 | 1 | } |
|
86 | |||
87 | 1 | return $string; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Check if a string start with another substring. |
||
92 | * |
||
93 | * @param string $string |
||
94 | * @param string $search |
||
95 | * @return bool |
||
96 | */ |
||
97 | 3 | public static function startsWith($string, $search) |
|
101 | |||
102 | /** |
||
103 | * Check if a string ends with another substring. |
||
104 | * |
||
105 | * @param string $string |
||
106 | * @param string $search |
||
107 | * @return bool |
||
108 | */ |
||
109 | 4 | public static function endsWith($string, $search) |
|
113 | |||
114 | /** |
||
115 | * Remove left part of string and return the new string. |
||
116 | * |
||
117 | * @param string $string |
||
118 | * @param string $strip |
||
119 | * @return string |
||
120 | */ |
||
121 | 1 | public static function stripLeft($string, $strip) |
|
129 | |||
130 | /** |
||
131 | * Remove right part of string and return the new string. |
||
132 | * |
||
133 | * @param string $string |
||
134 | * @param string $strip |
||
135 | * @return string |
||
136 | */ |
||
137 | 2 | public static function stripRight($string, $strip) |
|
145 | |||
146 | /** |
||
147 | * Increment a string/separated string. |
||
148 | * Example: "a-name" becomes "a-name-2", "a-name-3" and so on. |
||
149 | * |
||
150 | * @param string $string |
||
151 | * @param string $separator |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public static function incrementSeparated($string, $separator = '-') |
|
155 | { |
||
156 | 1 | if (preg_match('/' . $separator . '(\d+)$/', $string, $matches)) { |
|
157 | 1 | $string = self::stripRight($string, $separator . $matches[1]); |
|
158 | 1 | $duplicateNo = ((int) $matches[1]) + 1; |
|
159 | 1 | } else { |
|
160 | 1 | $duplicateNo = 2; |
|
161 | } |
||
162 | |||
163 | 1 | $newString = $string . $separator . $duplicateNo; |
|
164 | |||
165 | 1 | return $newString; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $string |
||
170 | * @return string |
||
171 | */ |
||
172 | 1 | public static function uppercaseFirst($string) |
|
180 | |||
181 | /** |
||
182 | * Filter out all non numeric characters and return a clean numeric string. |
||
183 | * |
||
184 | * @param string $string |
||
185 | * @param bool $allowDecimal |
||
186 | * @param bool $allowNegative |
||
187 | * @return string |
||
188 | */ |
||
189 | 3 | public static function toNumber($string, $allowDecimal = false, $allowNegative = false) |
|
223 | |||
224 | /** |
||
225 | * @param string $search |
||
226 | * @param string $replace |
||
227 | * @param string $subject |
||
228 | * @return string |
||
229 | */ |
||
230 | 2 | public static function replaceFirst($search, $replace, $subject) |
|
238 | } |
||
239 |