Str Class

Table of Contents

  1. Overview
  2. after
  3. ascii
  4. base64Encode
  5. base64Decode
  6. before
  7. between
  8. camel
  9. chunk
  10. compare
  11. contains
  12. crc32
  13. endsWith
  14. excerpt
  15. finish
  16. headline
  17. isAscii
  18. isEmpty
  19. isJson
  20. isUuid
  21. kebab
  22. lastPosition
  23. lcfirst
  24. length
  25. levenshtein
  26. limit
  27. lower
  28. mask
  29. md5
  30. numberFormat
  31. padLeft
  32. padRight
  33. pascal
  34. plural
  35. position
  36. random
  37. repeat
  38. replaceArray
  39. replaceFirst
  40. replaceLast
  41. replaceMultiple
  42. reverse
  43. sha1
  44. shuffle
  45. similarity
  46. snake
  47. slug
  48. squish
  49. startsWith
  50. stripWhitespace
  51. studly
  52. substr
  53. substrCount
  54. swapKeyValue
  55. title
  56. toArray
  57. ucfirst
  58. upper
  59. uuid
  60. wordCount
  61. words
  62. wrap

1. Overview Table of Contents

Overview of the Str utility class providing various methods for string manipulation and checks.

2. after Table of Contents

after(string $subject, string $search): string

Returns the portion of the string after the first occurrence of a given substring.

Str::after('hello world', 'hello'); // ' world'


3. ascii Table of Contents

ascii(string $value): string

Converts a string to its ASCII representation.

Str::ascii('ü'); // 'u'


4. base64Encode Table of Contents

base64Encode(string $value): string

Encodes a string using base64.

Str::base64Encode('hello'); // 'aGVsbG8='


5. base64Decode Table of Contents

base64Decode(string $value): string

Decodes a base64 encoded string.

Str::base64Decode('aGVsbG8='); // 'hello'


6. before Table of Contents

before(string $subject, string $search): string

Returns the portion of the string before the first occurrence of a given substring.

Str::before('hello world', 'world'); // 'hello '


7. between Table of Contents

between(string $value, string $start, string $end): string

Returns the substring between two substrings.

Str::between('[a] b [c]', '[', ']'); // 'a'


8. camel Table of Contents

camel(string $value): string

Converts a string to camelCase.

Str::camel('hello_world'); // 'helloWorld'


9. chunk Table of Contents

chunk(string $value, int $length = 1): array

Splits a string into chunks.

Str::chunk('hello', 2); // ['he', 'll', 'o']


10. compare Table of Contents

compare(string $string1, string $string2): int

Compares two strings.

Str::compare('abc', 'abc'); // 0
Str::compare('abc', 'xyz'); // negative integer


11. contains Table of Contents

contains(string $haystack, string $needle): bool

Determines if a string contains a given substring.

Str::contains('hello world', 'world'); // true


12. crc32 Table of Contents

crc32(string $value): int

Calculates the CRC32 hash of a string.

Str::crc32('hello'); // e.g., 907060870


13. endsWith Table of Contents

endsWith(string $haystack, string $needle): bool

Checks if a string ends with a given substring.

Str::endsWith('hello world', 'world'); // true


14. excerpt Table of Contents

excerpt(string $text, string $phrase, int $radius = 100): string

Creates an excerpt around a phrase.

Str::excerpt('This is a long sentence.', 'long', 5); // 'is a long sente'


15. finish Table of Contents

finish(string $value, string $cap): string

Ensures a string ends with a given value.

Str::finish('hello', '!'); // 'hello!'


16. headline Table of Contents

headline(string $value): string

Converts a string to headline case.

Str::headline('hello_world'); // 'Hello World'


17. isAscii Table of Contents

isAscii(string $value): bool

Checks if the string contains only ASCII characters.

Str::isAscii('abc'); // true
Str::isAscii('ü'); // false


18. isEmpty Table of Contents

isEmpty(string $value): bool

Determines if a string is empty.

Str::isEmpty(''); // true
Str::isEmpty(' '); // true


19. isJson Table of Contents

isJson(string $value): bool

Checks if a string is valid JSON.

Str::isJson('{"key":"value"}'); // true


20. isUuid Table of Contents

isUuid(string $value): bool

Determines if the given string is a valid UUID.

Str::isUuid('550e8400-e29b-41d4-a716-446655440000'); // true


21.kebab Table of Contents

kebab(string $value): string

Converts a string to kebab-case.

Str::kebab('Hello World'); // 'hello-world'


22. lastPosition Table of Contents

lastPosition(string $haystack, string $needle): int|false

Finds the position of the last occurrence of a substring.

Str::lastPosition('Hello World', 'o'); // 7


23. lcfirst Table of Contents

lcfirst(string $value): string

Converts the first character of a string to lowercase.

Str::lcfirst('Hello'); // 'hello'


24. length Table of Contents

length(string $value): int

Returns the length of a string.

Str::length('Hello'); // 5


25. levenshtein Table of Contents

levenshtein(string $string1, string $string2): int

Calculates the Levenshtein distance between two strings.

Str::levenshtein('kitten', 'sitting'); // 3


26. limit Table of Contents

limit(string $value, int $limit = 100, string $end = '...'): string

Limits the number of characters in a string.

Str::limit('Hello World', 5); // 'Hello...'


27. lower Table of Contents

lower(string $value): string

Converts a string to lowercase.

Str::lower('Hello'); // 'hello'


28. mask Table of Contents

mask(string $string, string $character = '*', int $start = 0, ?int $length = null): string

Masks portions of a string with a given character.

Str::mask('1234567890', '*', 2, 5); // '12*****890'


29. md5 Table of Contents

md5(string $value): string

Generates the MD5 hash of a string.

Str::md5('hello'); // '5d41402abc4b2a76b9719d911017c592'


30. numberFormat Table of Contents

numberFormat(float $number, int $decimals = 0, string $decimalSeparator = '.', string $thousandSeparator = ','): string

Formats a number with grouped thousands.

Str::numberFormat(12345.678, 2); // '12,345.68'


31. padLeft Table of Contents

padLeft(string $value, int $length, string $pad = ' '): string

Pads the left side of a string to a specified length with a given character.

Str::padLeft('hello', 8, '_'); // '___hello'


32. padRight Table of Contents

padRight(string $value, int $length, string $pad = ' '): string

Pads the right side of a string to a specified length with a given character.

Str::padRight('Hello', 10, '-'); // 'Hello-----'


33. pascal Table of Contents

pascal(string $value): string

Converts a string to PascalCase.

Str::pascal('hello world'); // 'HelloWorld'


34. plural Table of Contents

plural(string $word, int $count = 2): string

Pluralizes a given word based on the count.

Str::plural('apple', 1); // 'apple'
Str::plural('apple', 2); // 'apples'


35. position Table of Contents

position(string $haystack, string $needle): int|false

Finds the position of the first occurrence of a substring.

Str::position('Hello World', 'World'); // 6


36. random Table of Contents

random(int $length = 16): string

Generates a random string of specified length.

Str::random(8); // e.g., '4f9d2c8a'


37. repeat Table of Contents

repeat(string $value, int $times): string

Repeats a given string a specified number of times.

Str::repeat('abc', 3); // 'abcabcabc'


38. replaceArray Table of Contents

replaceArray(string $search, array $replace, string $subject): string

Sequentially replaces placeholders with values from an array.

Str::replaceArray('?', ['one', 'two'], '? ?'); // 'one two'


39. replaceFirst Table of Contents

replaceFirst(string $search, string $replace, string $subject): string

Replaces the first occurrence of a substring.

Str::replaceFirst('cat', 'dog', 'cat cat'); // 'cat dog'


40. replaceLast Table of Contents

replaceLast(string $search, string $replace, string $subject): string

Replaces the last occurrence of a substring.

Str::replaceLast('apple', 'orange', 'apple pie, apple pie'); // 'apple pie, orange pie'


41. replaceMultiple Table of Contents

replaceMultiple(array $replacements, string $subject): string

Replaces multiple substrings simultaneously.

Str::replaceMultiple(['cat' => 'dog', 'blue' => 'red'], 'cat and dog'); // 'cat dog'


42. reverse Table of Contents

reverse(string $value): string

Reverses the given string.

Str::reverse('hello'); // 'olleh'


43. sha1 Table of Contents

sha1(string $value): string

Returns the SHA1 hash of a string.

Str::sha1('hello'); // 'f7ff9e8b7bb2e09b70935d20b8a76a62cbd30d2f'


44. shuffle Table of Contents

shuffle(string $value): string

Randomly shuffles the characters in a string.

Str::shuffle('hello'); // e.g., 'eholl'


45. similarity Table of Contents

similarity(string $string1, string $string2): int

Calculates similarity percentage between two strings.

Str::similarity('hello', 'hallo'); // e.g., 80


46. snake Table of Contents

snake(string $value, string $delimiter = '_'): string

Converts a string to snake_case.

Str::snake('Hello World'); // 'hello_world'


47. slug Table of Contents

slug(string $title, string $separator = '-'): string

Creates a URL-friendly slug from a given string.

Str::slug('Hello World!'); // 'hello-world'


48. squish Table of Contents

squish(string $value): string

Removes excessive whitespace from a string.

Str::squish('  Hello    World  '); // 'Hello World'


49. startsWith Table of Contents

startsWith(string $haystack, string $needle): bool

Determines if a string starts with a given substring.

Str::startsWith('Hello World', 'Hello'); // true


50. stripWhitespace Table of Contents

stripWhitespace(string $value): string

Removes all whitespace from a given string.

Str::stripWhitespace('Hello World'); // 'HelloWorld'


51. studly Table of Contents

studly(string $value): string

Converts a string to StudlyCase (PascalCase).

Str::studly('hello_world'); // 'HelloWorld'


52. substr Table of Contents

substr(string $value, int $start, ?int $length = null): string

Extracts a substring from a given string.

Str::substr('Hello World', 0, 5); // 'Hello'


53. substrCount Table of Contents

substrCount(string $haystack, string $needle): int

Counts the number of occurrences of a substring within a string.

Str::substrCount('apple pie apple', 'apple'); // 2


54. swapKeyValue Table of Contents

swapKeyValue(array $array): string

Swaps keys with values in an array and returns a formatted string.

Str::swapKeyValue(['a' => 1, 'b' => 2]); // '1 => a, 2 => b'


55. title Table of Contents

title(string $value): string

Converts a string to title case.

Str::title('hello world'); // 'Hello World'


56. toArray Table of Contents

toArray(string $value): array

Splits a string into an array of characters.

Str::toArray('Hello'); // ['H', 'e', 'l', 'l', 'o']


57. ucfirst Table of Contents

ucfirst(string $value): string

Capitalizes the first character of a string.

Str::ucfirst('hello'); // 'Hello'


58. upper Table of Contents

upper(string $value): string

Converts a string to uppercase.

Str::upper('hello'); // 'HELLO'


59. uuid Table of Contents

uuid(): string

Generates a UUID (Universally Unique Identifier).

Str::uuid(); // '550e8400-e29b-41d4-a716-446655440000'


60. wordCount Table of Contents

wordCount(string $value): int

Counts the number of words in a string.

Str::wordCount('Hello world'); // 2


61. words Table of Contents

words(string $value, int $words = 10, string $end = '...'): string

Limits a string to a certain number of words.

Str::words('Hello world of PHP', 2); // 'Hello world...'


62. wrap Table of Contents

wrap(string $value, string $wrapWith): string

Wraps a string with a given value.

Str::wrap('hello', '*'); // '*hello*'