1 | <?php |
||
9 | final class Strings { |
||
10 | private static $controlCharMap = [ |
||
11 | "\n" => '\n', |
||
12 | "\r" => '\r', |
||
13 | "\t" => '\t', |
||
14 | "\v" => '\v', |
||
15 | "\e" => '\e', |
||
16 | "\f" => '\f', |
||
17 | ]; |
||
18 | |||
19 | const CONTROL_CHAR_PATTERN = '/[\x00-\x1F\x7F]/'; |
||
20 | |||
21 | /** |
||
22 | * startsWith returns true if the input begins with a prefix. |
||
23 | * |
||
24 | * @param string $input |
||
25 | * @param string $prefix |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | public static function startsWith($input, $prefix) { |
||
32 | |||
33 | /** |
||
34 | * endsWith returns true if the input ends with a suffix. |
||
35 | * |
||
36 | * @param string $input |
||
37 | * @param string $suffix |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | public static function endsWith($input, $suffix) { |
||
44 | |||
45 | /** |
||
46 | * getMostFrequentNeedle by counting occurences of each needle in haystack. |
||
47 | * |
||
48 | * @param string $haystack Haystack to be searched in. |
||
49 | * @param array $needles Needles to be counted. |
||
50 | * |
||
51 | * @return string|null The most occuring needle. If counts are tied, the first tied needle is returned. If no |
||
52 | * needles were found, `null` is returned. |
||
53 | */ |
||
54 | public static function getMostFrequentNeedle($haystack, array $needles) { |
||
66 | |||
67 | /** |
||
68 | * escapeControlChars by replacing line feeds, tabs, etc. to their escaped representation. |
||
69 | * |
||
70 | * e.g. an actual line feed will return '\n' |
||
71 | * |
||
72 | * @param string $input |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public static function escapeControlChars($input) { |
||
87 | |||
88 | /** |
||
89 | * padMultibyte strings to a certain length with another string. |
||
90 | * |
||
91 | * @param string $input The input string to be padded. |
||
92 | * @param int $padLength If the pad is length smaller than the input length, no padding takes place. |
||
93 | * @param string $padding Optional, defaults to a space character. Can be more than one character. The padding |
||
94 | * may be truncated if the required number of padding characters can't be evenly |
||
95 | * divided. |
||
96 | * @param int $paddingType Optional, defaults to STR_PAD_RIGHT. Must be one of STR_PAD_LEFT, STR_PAD_RIGHT or |
||
97 | * STR_PAD_BOTH. |
||
98 | * |
||
99 | * @return string The padded string. |
||
100 | */ |
||
101 | public static function padMultibyte($input, $padLength, $padding = ' ', $paddingType = STR_PAD_RIGHT) { |
||
135 | |||
136 | /** |
||
137 | * getCommonPrefix of two strings. |
||
138 | * |
||
139 | * @param string $first |
||
140 | * @param string $second |
||
141 | * |
||
142 | * @return string All common characters from the beginning of both strings. |
||
143 | */ |
||
144 | public static function getCommonPrefix($first, $second) { |
||
156 | |||
157 | /** |
||
158 | * getCommonSuffix of two strings. |
||
159 | * |
||
160 | * @param string $first |
||
161 | * @param string $second |
||
162 | * |
||
163 | * @return string All common characters from the end of both strings. |
||
164 | */ |
||
165 | public static function getCommonSuffix($first, $second) { |
||
169 | |||
170 | /** |
||
171 | * Reverse a string. |
||
172 | * |
||
173 | * @param string $input |
||
174 | * |
||
175 | * @return string The reversed string. |
||
176 | */ |
||
177 | public static function reverse($input) { |
||
185 | |||
186 | /** |
||
187 | * groupByCommonPrefix returns an array with a common key and a list of differing suffixes. |
||
188 | * |
||
189 | * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
||
190 | * ``` |
||
191 | * 'same' => [ |
||
192 | * 'HERE', |
||
193 | * 'THERE', |
||
194 | * ] |
||
195 | * ``` |
||
196 | * |
||
197 | * This can be used to group several file paths by a common base. |
||
198 | * |
||
199 | * @param string[] $strings |
||
200 | * |
||
201 | * @return string[][] |
||
202 | */ |
||
203 | public static function groupByCommonPrefix($strings) { |
||
220 | |||
221 | /** |
||
222 | * groupByCommonSuffix returns an array with a common key and a list of differing suffixes. |
||
223 | * |
||
224 | * e.g. passing an array `['sameHERE', 'sameTHERE']` would return |
||
225 | * ``` |
||
226 | * 'HERE' => [ |
||
227 | * 'same', |
||
228 | * 'sameT', |
||
229 | * ] |
||
230 | * ``` |
||
231 | * |
||
232 | * @param string[] $strings |
||
233 | * |
||
234 | * @return string[][] |
||
235 | */ |
||
236 | public static function groupByCommonSuffix($strings) { |
||
247 | } |
||
248 |