| Total Complexity | 90 |
| Total Lines | 729 |
| Duplicated Lines | 0 % |
| Changes | 36 | ||
| Bugs | 3 | Features | 32 |
Complex classes like Strings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Strings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Strings |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * The cache for words. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected static $cache = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Removes any leading and traling slashes from a string. |
||
| 64 | * |
||
| 65 | * @param string $string String with slashes |
||
| 66 | */ |
||
| 67 | public static function trimSlashes(string $string): string |
||
| 68 | { |
||
| 69 | return static::trim($string, '/'); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Reduces multiple slashes in a string to single slashes. |
||
| 74 | * |
||
| 75 | * @param string $string String or array of strings with slashes |
||
| 76 | */ |
||
| 77 | public static function reduceSlashes(string $string): string |
||
| 78 | { |
||
| 79 | return preg_replace('#(?<!:)//+#', '/', $string); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Removes single and double quotes from a string. |
||
| 84 | * |
||
| 85 | * @param string $str String with single and double quotes |
||
| 86 | */ |
||
| 87 | public static function stripQuotes(string $string): string |
||
| 88 | { |
||
| 89 | return str_replace(['"', "'"], '', $string); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Convert single and double quotes to entities. |
||
| 94 | * |
||
| 95 | * @param string $string String with single and double quotes |
||
| 96 | */ |
||
| 97 | public static function quotesToEntities(string $string): string |
||
| 98 | { |
||
| 99 | return str_replace(["\'", '"', "'", '"'], [''', '"', ''', '"'], $string); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Checks if the string is valid in UTF-8 encoding. |
||
| 104 | * |
||
| 105 | * @param string $string String |
||
| 106 | */ |
||
| 107 | public static function validEncoding(string $string): bool |
||
| 108 | { |
||
| 109 | return $string === static::fixEncoding($string); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Removes all invalid UTF-8 characters from a string. |
||
| 114 | * |
||
| 115 | * @param string $string String |
||
| 116 | */ |
||
| 117 | public static function fixEncoding(string $string): string |
||
| 118 | { |
||
| 119 | return htmlspecialchars_decode(htmlspecialchars($string, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Standardize line endings to unix-like. |
||
| 124 | * |
||
| 125 | * @param string $string String |
||
| 126 | */ |
||
| 127 | public static function normalizeNewLines(string $string): string |
||
| 128 | { |
||
| 129 | return str_replace(["\r\n", "\r"], "\n", $string); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Normalize white-spaces to a single space. |
||
| 134 | * |
||
| 135 | * @param string $string String |
||
| 136 | */ |
||
| 137 | public static function normalizeSpaces(string $string): string |
||
| 138 | { |
||
| 139 | return preg_replace('/\s+/', ' ', $string); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a random string of characters. |
||
| 144 | * |
||
| 145 | * @param int $length The number of characters. Default is 16 |
||
| 146 | * @param string $keyspace The keyspace |
||
| 147 | */ |
||
| 148 | public static function random(int $length = 64, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'): string |
||
| 149 | { |
||
| 150 | if ($length <= 0) { |
||
| 151 | $length = 1; |
||
| 152 | } |
||
| 153 | |||
| 154 | $pieces = []; |
||
| 155 | $max = static::length($keyspace, '8bit') - 1; |
||
| 156 | |||
| 157 | for ($i = 0; $i < $length; ++$i) { |
||
| 158 | $pieces[] = $keyspace[random_int(0, $max)]; |
||
| 159 | } |
||
| 160 | |||
| 161 | return implode('', $pieces); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc. |
||
| 166 | * |
||
| 167 | * @param string $string String to increment |
||
| 168 | * @param int $first Start with |
||
| 169 | * @param string $separator Separator |
||
| 170 | */ |
||
| 171 | public static function increment(string $string, int $first = 1, string $separator = '_'): string |
||
| 172 | { |
||
| 173 | preg_match('/(.+)' . $separator . '([0-9]+)$/', $string, $match); |
||
| 174 | |||
| 175 | return isset($match[2]) ? $match[1] . $separator . ($match[2] + 1) : $string . $separator . $first; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return the length of the given string. |
||
| 180 | * |
||
| 181 | * @param string $string String to check |
||
| 182 | * @param string|null $encoding String encoding |
||
| 183 | */ |
||
| 184 | public static function length(string $string, ?string $encoding = null): int |
||
| 185 | { |
||
| 186 | if ($encoding) { |
||
| 187 | return mb_strlen($string, $encoding); |
||
| 188 | } |
||
| 189 | |||
| 190 | return mb_strlen($string); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Limit the number of characters in a string. |
||
| 195 | * |
||
| 196 | * @param string $string String |
||
| 197 | * @param int $limit Limit of characters |
||
| 198 | * @param string $append Text to append to the string IF it gets truncated |
||
| 199 | */ |
||
| 200 | public static function limit(string $string, int $limit = 100, string $append = '...'): string |
||
| 201 | { |
||
| 202 | if (mb_strwidth($string, 'UTF-8') <= $limit) { |
||
| 203 | return $string; |
||
| 204 | } |
||
| 205 | |||
| 206 | return rtrim(mb_strimwidth($string, 0, $limit, '', 'UTF-8')) . $append; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Convert the given string to lower-case. |
||
| 211 | * |
||
| 212 | * @param string $string String |
||
| 213 | */ |
||
| 214 | public static function lower(string $string): string |
||
| 215 | { |
||
| 216 | return mb_strtolower($string, 'UTF-8'); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Convert the given string to upper-case. |
||
| 221 | * |
||
| 222 | * @param string $value |
||
| 223 | */ |
||
| 224 | public static function upper(string $string): string |
||
| 225 | { |
||
| 226 | return mb_strtoupper($string, 'UTF-8'); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the portion of string specified by the start and length parameters. |
||
| 231 | * |
||
| 232 | * @param string $string The string to extract the substring from. |
||
| 233 | * @param int $start If start is non-negative, the returned string will |
||
| 234 | * start at the start'th position in $string, counting from zero. |
||
| 235 | * For instance, in the string 'abcdef', the character at position |
||
| 236 | * 0 is 'a', the character at position 2 is 'c', and so forth. |
||
| 237 | * @param int|null $length Maximum number of characters to use from string. |
||
| 238 | * If omitted or NULL is passed, extract all characters to the end of the string. |
||
| 239 | */ |
||
| 240 | public static function substr(string $string, int $start, ?int $length = null): string |
||
| 241 | { |
||
| 242 | return mb_substr($string, $start, $length, 'UTF-8'); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Convert a string to studly caps case. |
||
| 247 | * |
||
| 248 | * @param string $string String |
||
| 249 | */ |
||
| 250 | public static function studly(string $string): string |
||
| 251 | { |
||
| 252 | $key = $string; |
||
| 253 | |||
| 254 | if (isset(static::$cache['studly'][$key])) { |
||
| 255 | return static::$cache['studly'][$key]; |
||
| 256 | } |
||
| 257 | |||
| 258 | $string = ucwords(str_replace(['-', '_'], ' ', $string)); |
||
| 259 | |||
| 260 | return static::$cache['studly'][$key] = str_replace(' ', '', $string); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Convert a string to snake case. |
||
| 265 | * |
||
| 266 | * @param string $string String |
||
| 267 | * @param string $delimiter Delimeter |
||
| 268 | */ |
||
| 269 | public static function snake(string $string, string $delimiter = '_'): string |
||
| 270 | { |
||
| 271 | $key = $string; |
||
| 272 | |||
| 273 | if (isset(static::$cache['snake'][$key][$delimiter])) { |
||
| 274 | return static::$cache['snake'][$key][$delimiter]; |
||
| 275 | } |
||
| 276 | |||
| 277 | if (! ctype_lower($string)) { |
||
| 278 | $string = preg_replace('/\s+/u', '', ucwords($string)); |
||
| 279 | |||
| 280 | $string = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $string)); |
||
| 281 | } |
||
| 282 | |||
| 283 | return static::$cache['snake'][$key][$delimiter] = $string; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Convert a string to camel case. |
||
| 288 | * |
||
| 289 | * @param string $string String |
||
| 290 | */ |
||
| 291 | public static function camel(string $string): string |
||
| 292 | { |
||
| 293 | if (isset(static::$cache['camel'][$string])) { |
||
| 294 | return static::$cache['camel'][$string]; |
||
| 295 | } |
||
| 296 | |||
| 297 | return static::$cache['camel'][$string] = lcfirst(static::studly($string)); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Convert a string to kebab case. |
||
| 302 | * |
||
| 303 | * @param string $string String |
||
| 304 | */ |
||
| 305 | public static function kebab(string $string): string |
||
| 306 | { |
||
| 307 | return static::snake($string, '-'); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Limit the number of words in a string. |
||
| 312 | * |
||
| 313 | * @param string $string String |
||
| 314 | * @param int $words Words limit |
||
| 315 | * @param string $append Text to append to the string IF it gets truncated |
||
| 316 | */ |
||
| 317 | public static function words(string $string, int $words = 100, string $append = '...'): string |
||
| 318 | { |
||
| 319 | preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $string, $matches); |
||
| 320 | |||
| 321 | if (! isset($matches[0]) || static::length($string) === static::length($matches[0])) { |
||
| 322 | return $string; |
||
| 323 | } |
||
| 324 | |||
| 325 | return static::trimRight($matches[0]) . $append; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Return information about words used in a string |
||
| 330 | * |
||
| 331 | * @param string $string String |
||
| 332 | * @param int $format Specify the return value of this function. The current supported values are: |
||
| 333 | * 0 - returns the number of words found |
||
| 334 | * 1 - returns an array containing all the words found inside the string |
||
| 335 | * 2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself |
||
| 336 | * @param string $charlist A list of additional characters which will be considered as 'word' |
||
| 337 | */ |
||
| 338 | public static function wordsCount(string $string, int $format = 0, string $charlist = '') |
||
| 339 | { |
||
| 340 | return str_word_count($string, $format, $charlist); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Determine if a given string contains a given substring. |
||
| 345 | * |
||
| 346 | * @param string $haystack The string being checked. |
||
| 347 | * @param string|string[] $needles The string to find in haystack. |
||
| 348 | */ |
||
| 349 | public static function contains(string $haystack, $needles): bool |
||
| 350 | { |
||
| 351 | foreach ((array) $needles as $needle) { |
||
| 352 | if ($needle !== '' && mb_strpos($haystack, $needle) !== false) { |
||
| 353 | return true; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Determine if a given string contains all array values. |
||
| 362 | * |
||
| 363 | * @param string $haystack The string being checked. |
||
| 364 | * @param string[] $needles The array of strings to find in haystack. |
||
| 365 | */ |
||
| 366 | public static function containsAll(string $haystack, array $needles): bool |
||
| 367 | { |
||
| 368 | foreach ($needles as $needle) { |
||
| 369 | if (! static::contains($haystack, $needle)) { |
||
| 370 | return false; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return true; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Determine if a given string contains any of array values. |
||
| 379 | * |
||
| 380 | * @param string $haystack The string being checked. |
||
| 381 | * @param string[] $needles The array of strings to find in haystack. |
||
| 382 | */ |
||
| 383 | public static function containsAny(string $haystack, array $needles): bool |
||
| 384 | { |
||
| 385 | foreach ($needles as $needle) { |
||
| 386 | if (static::contains($haystack, $needle)) { |
||
| 387 | return true; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | return false; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Converts the first character of a string to upper case |
||
| 396 | * and leaves the other characters unchanged. |
||
| 397 | * |
||
| 398 | * @param string $string String |
||
| 399 | */ |
||
| 400 | public static function ucfirst(string $string): string |
||
| 401 | { |
||
| 402 | return static::upper(static::substr($string, 0, 1)) . static::substr($string, 1); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Converts the first character of every word of string to upper case and the others to lower case. |
||
| 407 | * |
||
| 408 | * @param string $string String |
||
| 409 | */ |
||
| 410 | public static function capitalize(string $string): string |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Strip whitespace (or other characters) from the beginning and end of a string. |
||
| 417 | * |
||
| 418 | * @param string $string The string that will be trimmed. |
||
| 419 | * @param string $character_mask Optionally, the stripped characters can also be |
||
| 420 | * specified using the character_mask parameter.. |
||
| 421 | */ |
||
| 422 | public static function trim(string $string, string $character_mask = " \t\n\r\0\x0B"): string |
||
| 423 | { |
||
| 424 | return trim($string, $character_mask); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Strip whitespace (or other characters) from the beginning of a string. |
||
| 429 | * |
||
| 430 | * @param string $string The string that will be trimmed. |
||
| 431 | * @param string $character_mask Optionally, the stripped characters can also be |
||
| 432 | * specified using the character_mask parameter.. |
||
| 433 | */ |
||
| 434 | public static function trimLeft(string $string, string $character_mask = " \t\n\r\0\x0B"): string |
||
| 435 | { |
||
| 436 | return ltrim($string, $character_mask); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Strip whitespace (or other characters) from the end of a string. |
||
| 441 | * |
||
| 442 | * @param string $string The string that will be trimmed. |
||
| 443 | * @param string $character_mask Optionally, the stripped characters can also be |
||
| 444 | * specified using the character_mask parameter.. |
||
| 445 | */ |
||
| 446 | public static function trimRight(string $string, string $character_mask = " \t\n\r\0\x0B"): string |
||
| 447 | { |
||
| 448 | return rtrim($string, $character_mask); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Reverses string. |
||
| 453 | * |
||
| 454 | * @param string $string String |
||
| 455 | */ |
||
| 456 | public static function reverse(string $string): string |
||
| 457 | { |
||
| 458 | $result = ''; |
||
| 459 | |||
| 460 | for ($i = static::length($string); $i >= 0; $i--) { |
||
| 461 | $result .= static::substr($string, $i, 1); |
||
| 462 | } |
||
| 463 | |||
| 464 | return $result; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get array of segments from a string based on a delimiter. |
||
| 469 | * |
||
| 470 | * @param string $string String |
||
| 471 | * @param string $delimiter Delimeter |
||
| 472 | */ |
||
| 473 | public static function segments(string $string, string $delimiter = ' '): array |
||
| 474 | { |
||
| 475 | return explode($delimiter, $string); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get a segment from a string based on a delimiter. |
||
| 480 | * Returns an empty string when the offset doesn't exist. |
||
| 481 | * Use a negative index to start counting from the last element. |
||
| 482 | * |
||
| 483 | * @param string $string String |
||
| 484 | * @param int $index Index |
||
| 485 | * @param string $delimiter Delimeter |
||
| 486 | */ |
||
| 487 | public static function segment(string $string, int $index, string $delimiter = ' '): string |
||
| 488 | { |
||
| 489 | $segments = explode($delimiter, $string); |
||
| 490 | |||
| 491 | if ($index < 0) { |
||
| 492 | $segments = array_reverse($segments); |
||
| 493 | $index = abs($index) - 1; |
||
| 494 | } |
||
| 495 | |||
| 496 | return $segments[$index] ?? ''; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the first segment from a string based on a delimiter. |
||
| 501 | * |
||
| 502 | * @param string $string String |
||
| 503 | * @param string $delimiter Delimeter |
||
| 504 | */ |
||
| 505 | public static function firstSegment(string $string, string $delimiter = ' '): string |
||
| 506 | { |
||
| 507 | return static::segment($string, 0, $delimiter); |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the last segment from a string based on a delimiter. |
||
| 512 | * |
||
| 513 | * @param string $string String |
||
| 514 | * @param string $delimiter Delimeter |
||
| 515 | */ |
||
| 516 | public static function lastSegment(string $string, string $delimiter = ' '): string |
||
| 517 | { |
||
| 518 | return static::segment($string, -1, $delimiter); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get the portion of a string between two given values. |
||
| 523 | * |
||
| 524 | * @param string $string String |
||
| 525 | * @param string $from From |
||
| 526 | * @param string $to To |
||
| 527 | */ |
||
| 528 | public static function between(string $string, string $from, string $to): string |
||
| 529 | { |
||
| 530 | if ($from === '' || $to === '') { |
||
| 531 | return $string; |
||
| 532 | } |
||
| 533 | |||
| 534 | return static::beforeLast(static::after($string, $from), $to); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get the portion of a string before the first occurrence of a given value. |
||
| 539 | * |
||
| 540 | * @param string $string String |
||
| 541 | * @param string $search Search |
||
| 542 | */ |
||
| 543 | public static function before(string $string, string $search): string |
||
| 544 | { |
||
| 545 | return $search === '' ? $string : explode($search, $string)[0]; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get the portion of a string before the last occurrence of a given value. |
||
| 550 | * |
||
| 551 | * @param string $string String |
||
| 552 | * @param string $search Search |
||
| 553 | */ |
||
| 554 | public static function beforeLast(string $string, string $search): string |
||
| 555 | { |
||
| 556 | if ($search === '') { |
||
| 557 | return $string; |
||
| 558 | } |
||
| 559 | |||
| 560 | $position = mb_strrpos($string, $search); |
||
| 561 | |||
| 562 | if ($position === false) { |
||
| 563 | return $string; |
||
| 564 | } |
||
| 565 | |||
| 566 | return static::substr($string, 0, $position); |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Return the remainder of a string after the first occurrence of a given value. |
||
| 571 | * |
||
| 572 | * @param string $string String |
||
| 573 | * @param string $search Search |
||
| 574 | */ |
||
| 575 | public static function after(string $string, string $search): string |
||
| 576 | { |
||
| 577 | return $search === '' ? $string : array_reverse(explode($search, $string, 2))[0]; |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Return the remainder of a string after the last occurrence of a given value. |
||
| 582 | * |
||
| 583 | * @param string $string String |
||
| 584 | * @param string $search Search |
||
| 585 | */ |
||
| 586 | public static function afterLast(string $string, string $search): string |
||
| 587 | { |
||
| 588 | if ($search === '') { |
||
| 589 | return $string; |
||
| 590 | } |
||
| 591 | |||
| 592 | $position = mb_strrpos($string, (string) $search); |
||
| 593 | |||
| 594 | if ($position === false) { |
||
| 595 | return $string; |
||
| 596 | } |
||
| 597 | |||
| 598 | return static::substr($string, $position + static::length($search)); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Pad both sides of a string with another. |
||
| 603 | * |
||
| 604 | * @param string $string The input string. |
||
| 605 | * @param int $length If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned. |
||
| 606 | * @param string $pad The pad string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length. |
||
| 607 | */ |
||
| 608 | public static function padBoth(string $string, int $length, string $pad = ' '): string |
||
| 609 | { |
||
| 610 | return str_pad($string, $length, $pad, STR_PAD_BOTH); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Pad the left side of a string with another. |
||
| 615 | * |
||
| 616 | * @param string $string The input string. |
||
| 617 | * @param int $length If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned. |
||
| 618 | * @param string $pad The pad string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length. |
||
| 619 | */ |
||
| 620 | public static function padLeft(string $string, int $length, string $pad = ' '): string |
||
| 621 | { |
||
| 622 | return str_pad($string, $length, $pad, STR_PAD_LEFT); |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Pad the right side of a string with another. |
||
| 627 | * |
||
| 628 | * @param string $string The input string. |
||
| 629 | * @param int $length If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned. |
||
| 630 | * @param string $pad The pad string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length. |
||
| 631 | */ |
||
| 632 | public static function padRight(string $string, int $length, string $pad = ' '): string |
||
| 633 | { |
||
| 634 | return str_pad($string, $length, $pad, STR_PAD_RIGHT); |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Strip all whitespaces from the given string. |
||
| 639 | * |
||
| 640 | * @param string $string The string to strip |
||
| 641 | */ |
||
| 642 | public static function stripSpaces(string $string): string |
||
| 643 | { |
||
| 644 | return preg_replace('/\s+/', '', $string); |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Replace a given value in the string sequentially with an array. |
||
| 649 | * |
||
| 650 | * @param string $string String |
||
| 651 | * @param string $search Search |
||
| 652 | * @param array $replace Replace |
||
| 653 | */ |
||
| 654 | public static function replaceArray(string $string, string $search, array $replace): string |
||
| 655 | { |
||
| 656 | $segments = explode($search, $string); |
||
| 657 | |||
| 658 | $result = array_shift($segments); |
||
| 659 | |||
| 660 | foreach ($segments as $segment) { |
||
| 661 | $result .= (array_shift($replace) ?? $search) . $segment; |
||
| 662 | } |
||
| 663 | |||
| 664 | return $result; |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Replace the first occurrence of a given value in the string. |
||
| 669 | * |
||
| 670 | * @param string $string String |
||
| 671 | * @param string $search Search |
||
| 672 | * @param string $replace Replace |
||
| 673 | */ |
||
| 674 | public static function replaceFirst(string $string, string $search, string $replace): string |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Replace the last occurrence of a given value in the string. |
||
| 691 | * |
||
| 692 | * @param string $string String |
||
| 693 | * @param string $search Search |
||
| 694 | * @param string $replace Replace |
||
| 695 | */ |
||
| 696 | public static function replaceLast(string $string, string $search, string $replace): string |
||
| 697 | { |
||
| 698 | $position = strrpos($string, $search); |
||
| 699 | |||
| 700 | if ($position !== false) { |
||
| 701 | return substr_replace($string, $replace, $position, strlen($search)); |
||
| 702 | } |
||
| 703 | |||
| 704 | return $subject; |
||
|
|
|||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Begin a string with a single instance of a given value. |
||
| 709 | * |
||
| 710 | * @param string $string String |
||
| 711 | * @param string $prefix Prefix |
||
| 712 | */ |
||
| 713 | public static function start(string $string, string $prefix): string |
||
| 714 | { |
||
| 715 | $quoted = preg_quote($prefix, '/'); |
||
| 716 | |||
| 717 | return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $string); |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Determine if a given string starts with a given substring. |
||
| 722 | * |
||
| 723 | * @param string $haystack Haystack |
||
| 724 | * @param string|string[] $needles needles |
||
| 725 | */ |
||
| 726 | public static function startsWith(string $haystack, $needles): bool |
||
| 727 | { |
||
| 728 | foreach ((array) $needles as $needle) { |
||
| 729 | if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) { |
||
| 730 | return true; |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | return false; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Determine if a given string ends with a given substring. |
||
| 739 | * |
||
| 740 | * @param string $haystack Haystack |
||
| 741 | * @param string|string[] $needles needles |
||
| 742 | */ |
||
| 743 | public static function endsWith(string $haystack, $needles): bool |
||
| 744 | { |
||
| 745 | foreach ((array) $needles as $needle) { |
||
| 746 | if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) { |
||
| 747 | return true; |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 751 | return false; |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Cap a string with a single instance of a given value. |
||
| 756 | * |
||
| 757 | * @param string $string String |
||
| 758 | * @param string $cap Cap |
||
| 759 | */ |
||
| 760 | public static function finish(string $string, string $cap): string |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Generate a hash string from the input string. |
||
| 769 | * |
||
| 770 | * @param string $string String |
||
| 771 | * @param string $algorithm Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). |
||
| 772 | * For a list of supported algorithms see hash_algos(). Default is md5. |
||
| 773 | * @param string $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. Default is FALSE |
||
| 774 | */ |
||
| 775 | public static function hash(string $string, string $algorithm = 'md5', bool $raw_output = false): string |
||
| 776 | { |
||
| 777 | if (in_array($algorithm, hash_algos())) { |
||
| 778 | return hash($algorithm, $string, $raw_output); |
||
| 779 | } |
||
| 780 | |||
| 781 | return $string; |
||
| 782 | } |
||
| 783 | } |
||
| 784 |