1 | <?php |
||
15 | class Strings |
||
16 | { |
||
17 | /** |
||
18 | * Create a random string with desired length. |
||
19 | * |
||
20 | * @param int $length String length. 32 symbols by default. |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | public static function random(int $length = 32): string |
||
32 | |||
33 | /** |
||
34 | * Return a URL safe version of a string. |
||
35 | * |
||
36 | * @param string $string |
||
37 | * @param string $separator |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public static function slug(string $string, string $separator = '-'): string |
||
45 | |||
46 | /** |
||
47 | * Applies htmlentities() and strip_tags() to string (if enabled). Can be used to clean up |
||
48 | * data before rendering it in HTML. |
||
49 | * |
||
50 | * @param string $string String to be escaped. |
||
51 | * @param bool $stripTags Will remove all tags using strip_tags(). Disabled by default. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public static function escape($string, bool $stripTags = false) |
||
67 | |||
68 | /** |
||
69 | * Shorter string with specified limit. UTF8 encoding will be used to support non English |
||
70 | * strings. |
||
71 | * |
||
72 | * @param string $string |
||
73 | * @param int $limit The max string length, 300 by default. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public static function shorter(string $string, int $limit = 300): string |
||
85 | |||
86 | /** |
||
87 | * Format bytes to human-readable format. |
||
88 | * |
||
89 | * @param int $bytes Size in bytes. |
||
90 | * @param int $decimals The number of decimals include to output. Set to 1 by default. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public static function bytes(int $bytes, int $decimals = 1): string |
||
103 | |||
104 | /** |
||
105 | * Normalize string endings to avoid EOL problem. Replace \n\r and multiply new lines with |
||
106 | * single \n. |
||
107 | * |
||
108 | * @param string $string String to be normalized. |
||
109 | * @param bool $joinMultiple Join multiple new lines into one. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public static function normalizeEndings(string $string, bool $joinMultiple = true): string |
||
121 | |||
122 | /** |
||
123 | * Shift all string lines to have minimum indent size set to 0. |
||
124 | * |
||
125 | * Example: |
||
126 | * |-a |
||
127 | * |--b |
||
128 | * |--c |
||
129 | * |---d |
||
130 | * |
||
131 | * Output: |
||
132 | * |a |
||
133 | * |-b |
||
134 | * |-c |
||
135 | * |--d |
||
136 | * |
||
137 | * @param string $string Input string with multiple lines. |
||
138 | * @param string $tabulationCost How to treat \t symbols relatively to spaces. By default, this |
||
139 | * is set to 4 spaces. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public static function normalizeIndents(string $string, string $tabulationCost = " "): string |
||
196 | } |