1 | <?php |
||
7 | class Str |
||
8 | { |
||
9 | |||
10 | const SNAKE_CASE = 1; |
||
11 | const CAMEL_CASE = 2; |
||
12 | |||
13 | const DIGITS = '0123456789'; |
||
14 | const ALPHABET_LOW = 'abcdefghijklmnopqrstuvwxyz'; |
||
15 | const ALPHABET_HIGH = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
16 | const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
17 | |||
18 | const RAND_ALPHA_LOW = 1; |
||
19 | const RAND_ALPHA_HIGH = 2; |
||
20 | const RAND_ALPHA = 4; |
||
21 | const RAND_DIGITS = 8; |
||
22 | const RAND_ALL = 16; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $dictionary = [ |
||
28 | 4 => self::ALPHABET . self::DIGITS, |
||
29 | 3 => self::DIGITS, |
||
30 | 2 => self::ALPHABET, |
||
31 | 1 => self::ALPHABET_HIGH, |
||
32 | 0 => self::ALPHABET_LOW, |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @param string $str |
||
37 | * @param int $length |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | 1 | public static function split($str, $length = 1) |
|
45 | |||
46 | /** |
||
47 | * Shortens text to length and keeps integrity of words |
||
48 | * |
||
49 | * @param string $str |
||
50 | * @param integer $length |
||
51 | * @param string $end |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 1 | public static function shorten($str, $length = 100, $end = '…') |
|
71 | |||
72 | /** |
||
73 | * @param string $string |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 1 | public static function ucFirst($string) |
|
84 | |||
85 | /** |
||
86 | * @param string $string |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 2 | public static function lcFirst($string) |
|
97 | |||
98 | /** |
||
99 | * Return random string |
||
100 | * |
||
101 | * @param int $length |
||
102 | * @param int $type |
||
103 | * |
||
104 | * @return string |
||
105 | * |
||
106 | * @throws \InvalidArgumentException |
||
107 | */ |
||
108 | 3 | public static function random($length = 32, $type = self::RAND_ALL) |
|
129 | |||
130 | /** |
||
131 | * @param string $chars |
||
132 | * @param int $length |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 2 | protected static function rand($chars, $length) |
|
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public static function uniqid() |
|
158 | |||
159 | /** |
||
160 | * @param int $size |
||
161 | * @param int $decimals |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | public static function fileSize($size, $decimals = 2) |
|
189 | |||
190 | /** |
||
191 | * transliteration cyr->lat |
||
192 | * |
||
193 | * @param $string |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 2 | public static function translit($string) |
|
203 | |||
204 | /** |
||
205 | * @param string $string |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function first($string) |
||
213 | |||
214 | /** |
||
215 | * @param string $string |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public static function withoutFirst($string) |
||
223 | |||
224 | /** |
||
225 | * @param string $string |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public static function last($string) |
||
233 | |||
234 | /** |
||
235 | * @param string $string |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public static function withoutLast($string) |
||
243 | |||
244 | /** |
||
245 | * @param string $string |
||
246 | * @param int $start |
||
247 | * @param int $length |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 3 | public static function sub($string, $start, $length = null) |
|
255 | |||
256 | /** |
||
257 | * @param string $string |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 3 | public static function upp($string) |
|
265 | |||
266 | /** |
||
267 | * @param string $string |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | 4 | public static function low($string) |
|
275 | |||
276 | /** |
||
277 | * @param string $string |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | 1 | public static function capitalize($string) |
|
285 | |||
286 | /** |
||
287 | * @param $string string |
||
288 | * @param $char string |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 1 | public static function snakeCase($string, $char = '_') |
|
298 | |||
299 | /** |
||
300 | * @param array $input |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 2 | final protected static function _upperCase(array $input) |
|
308 | |||
309 | /** |
||
310 | * @param string $string |
||
311 | * @param string $char |
||
312 | * @param bool $lcFirst |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | 2 | public static function camelCase($string, $char = '_', $lcFirst = false) |
|
331 | |||
332 | /** |
||
333 | * @param string $string |
||
334 | * @param bool $translit |
||
335 | * @param int $case |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | 1 | public static function friendlyUrl($string, $translit = true, $case = self::SNAKE_CASE) |
|
359 | |||
360 | /** |
||
361 | * @param string $string |
||
362 | * |
||
363 | * @return string mixed |
||
364 | */ |
||
365 | 1 | public static function toNumber($string) |
|
369 | |||
370 | /** |
||
371 | * @param string $string |
||
372 | * @param string $needle |
||
373 | * @param int $offset |
||
374 | * |
||
375 | * @return int |
||
376 | */ |
||
377 | 1 | public static function pos($string, $needle, $offset = null) |
|
381 | |||
382 | /** |
||
383 | * @param string $string |
||
384 | * @param int $multiplier |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | 1 | public static function repeat($string, $multiplier) |
|
392 | |||
393 | /** |
||
394 | * @param string $string |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 1 | public static function shuffle($string) |
|
402 | |||
403 | /** |
||
404 | * @param string $string |
||
405 | * |
||
406 | * @return int |
||
407 | */ |
||
408 | 2 | public static function len($string) |
|
412 | |||
413 | protected static $trans = [ |
||
414 | 'á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', |
||
415 | 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', |
||
416 | 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', |
||
417 | 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', |
||
418 | 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', |
||
419 | 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', |
||
420 | 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', |
||
421 | 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', |
||
422 | 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', |
||
423 | 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', |
||
424 | 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', |
||
425 | 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', |
||
426 | 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', |
||
427 | 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', |
||
428 | 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', |
||
429 | 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', |
||
430 | 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', |
||
431 | 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', |
||
432 | 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', |
||
433 | 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', |
||
434 | 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', |
||
435 | 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', |
||
436 | 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', |
||
437 | 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', |
||
438 | 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', |
||
439 | 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'A', |
||
440 | 'б' => 'b', 'Б' => 'B', 'в' => 'v', 'В' => 'V', 'г' => 'g', 'Г' => 'G', 'д' => 'd', 'Д' => 'D', |
||
441 | 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'Zh', 'з' => 'z', 'З' => 'Z', |
||
442 | 'и' => 'i', 'И' => 'I', 'й' => 'j', 'Й' => 'J', 'к' => 'k', 'К' => 'K', 'л' => 'l', 'Л' => 'L', |
||
443 | 'м' => 'm', 'М' => 'M', 'н' => 'n', 'Н' => 'N', 'о' => 'o', 'О' => 'O', 'п' => 'p', 'П' => 'P', |
||
444 | 'р' => 'r', 'Р' => 'R', 'с' => 's', 'С' => 'S', 'т' => 't', 'Т' => 'T', 'у' => 'u', 'У' => 'U', |
||
445 | 'ф' => 'f', 'Ф' => 'F', 'х' => 'h', 'Х' => 'H', 'ц' => 'c', 'Ц' => 'C', 'ч' => 'ch', 'Ч' => 'Ch', |
||
446 | 'ш' => 'sh', 'Ш' => 'Sh', 'щ' => 'sch', 'Щ' => 'Sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'Y', |
||
447 | 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'E', 'ю' => 'ju', 'Ю' => 'Ju', 'я' => 'ja', 'Я' => 'Ja' |
||
448 | ]; |
||
449 | |||
450 | } |
||
451 |