Str Class
Table of Contents
- Overview
- after()
- ascii()
- base64Encode()
- base64Decode()
- before()
- between()
- camel()
- chunk()
- compare()
- contains()
- crc32()
- endsWith()
- excerpt()
- finish()
- headline()
- isAscii()
- isEmpty()
- isJson()
- isUuid()
- kebab()
- lastPosition()
- lcfirst()
- length()
- levenshtein()
- limit()
- lower()
- mask()
- md5()
- numberFormat()
- padLeft()
- padRight()
- pascal()
- plural()
- position()
- random()
- repeat()
- replace()
- replaceArray()
- replaceFirst()
- replaceLast()
- replaceMultiple()
- reverse()
- sha1()
- shuffle()
- similarity()
- snake()
- slug()
- squish()
- startsWith()
- stripWhitespace()
- studly()
- substr()
- substrCount()
- swapKeyValue()
- title()
- toArray()
- ucfirst()
- ucwords()
- upper()
- uuid()
- wordCount()
- words()
- 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
Returns the portion of the string after the first occurrence of a given substring.
Parameters:
string $subject- The input string.string $search- The substring to search for.
Returns:
string- Portion of a string after first occurrence of a given value.
Example:
Str::after('hello world', 'hello'); // ' world'
3. ascii() Table of Contents
Converts a string to its ASCII representation.
Parameter:
string $value- The input string.
Returns:
string- The ASCII representation of a string.
Example:
Str::ascii('ü'); // 'u'
4. base64Encode() Table of Contents
Encodes a string using base64.
Parameter:
string $value- The input string.
Returns:
string- The Base64 encoded string.
Example:
Str::base64Encode('hello'); // 'aGVsbG8='
5. base64Decode() Table of Contents
Decodes a base64 encoded string.
Parameter:
string $value- The base64 encoded string.
Returns:
string- The decoded string.
Example:
Str::base64Decode('aGVsbG8='); // 'hello'
6. before() Table of Contents
Returns the portion of the string before the first occurrence of a given substring.
Parameter:
string $subject- The input string.string $search- The substring to search for.
Returns:
string- The portion of a string before the first occurrence of a given value.
Example:
Str::before('hello world', 'world'); // 'hello '
7. between() Table of Contents
Returns the substring between two substrings.
Parameters:
string $value- The input string.string $start- The starting substring.string $end- The ending substring.
Returns:
string- The substring between two given substrings.
Example:
Str::between('[a] b [c]', '[', ']'); // 'a'
8. camel() Table of Contents
Converts a string to camelCase.
Parameter:
string $value- The input string.
Returns:
string- The string in camelCase.
Example:
Str::camel('hello_world'); // 'helloWorld'
9. chunk() Table of Contents
Splits a string into chunks.
Parameters:
string $value- The input string.int $length- The chunk length.
Returns:
array- An array consisting of a string split into chunks.
Example:
Str::chunk('hello', 2); // ['he', 'll', 'o']
10. compare() Table of Contents
Compares two strings.
Parameters:
string $string1- The first string.string $string2- The second string.
Returns:
int- Less 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
Example:
Str::compare('abc', 'abc'); // 0
Str::compare('abc', 'xyz'); // negative integer
11. contains() Table of Contents
Determines if a string contains a given substring.
Parameters:
string $haystack- The string to search within.string $needle- The substring to search for.
Returns:
bool- True if substring exists, otherwise false.
Example:
Str::contains('hello world', 'world'); // true
12. crc32() Table of Contents
Calculates the CRC32 hash of a string.
Parameter:
string $value- The input string.
Returns:
int Returns- The crc32 checksum of string as an integer.
Example:
Str::crc32('hello'); // e.g., 907060870
13. endsWith() Table of Contents
Checks if a string ends with a given substring.
Parameters:
string $haystack- The string to check.string $needle- The substring to check for.
Returns:
bool- True if string ends with given substring, otherwise we return false.
Example:
Str::endsWith('hello world', 'world'); // true
14. excerpt() Table of Contents
Creates an excerpt around a phrase.
Parameters:
string $text- The input string.string $phrase- The phrase to excerpt around.int $radius- The number of characters around the phrase.
Returns:
string- The portion of str specified by the start and length parameters.
Example:
Str::excerpt('This is a long sentence.', 'long', 5); // 'is a long sente'
15. finish() Table of Contents
Ensures a string ends with a given value.
Parameters:
string $value- The input string.string $cap- The ending string to append if missing.
Returns:
string- The string with the desired ending.
Example:
Str::finish('hello', '!'); // 'hello!'
16. headline() Table of Contents
Converts a string to headline case.
Parameter:
string $value- The input string.
Returns:
string- The string in headline case.
Example:
Str::headline('hello_world'); // 'Hello World'
17. isAscii() Table of Contents
Checks if the string contains only ASCII characters.
Parameter:
string $value- The input string.
Returns:
bool- True on success or false on failure.
Example:
Str::isAscii('abc'); // true
Str::isAscii('ü'); // false
18. isEmpty() Table of Contents
Determines if a string is empty.
Parameter:
string $value- The input string.
Returns:
bool- True if empty string, otherwise false.
Example:
Str::isEmpty(''); // true
Str::isEmpty(' '); // true
19. isJson() Table of Contents
Checks if a string is valid JSON.
Parameter:
string $value- The input string.
Returns:
bool- True if valid JSON, otherwise false.
Example:
Str::isJson('{"key":"value"}'); // true
20. isUuid() Table of Contents
Determines if the given string is a valid UUID.
Parameter:
string $value- The input string.
Returns:
bool- True if the string is a valid UUID, false otherwise.
Example:
Str::isUuid('550e8400-e29b-41d4-a716-446655440000'); // true
21. kebab() Table of Contents
Converts a string to kebab-case.
Parameter:
string $value- The input string.
Returns:
string- The string in kebab-case.
Example:
Str::kebab('Hello World'); // 'hello-world'
22. lastPosition() Table of Contents
Finds the position of the last occurrence of a substring.
Parameters:
string $haystack- The string to search in.string $needle- The substring to search for.
Returns:
int|false- The position where the needle exists relative to the beginning of the haystack string (independent of search direction or offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found. Example:
Str::lastPosition('Hello World', 'o'); // 7
23. lcfirst() Table of Contents
Converts the first character of a string to lowercase.
Parameter:
string $value- The input string.
Returns:
string- The string with the first character converted to lowercase.
Example:
Str::lcfirst('Hello'); // 'hello'
24. length() Table of Contents
Returns the length of a string.
Parameter:
string $value- The input string.
Returns:
int- The number of characters in string str having character encoding. A multi-byte character is counted as 1.
Example:
Str::length('Hello'); // 5
25. levenshtein() Table of Contents
Calculates the Levenshtein distance between two strings.
Parameters:
string $string1- The first string.string $string2- The second string.
Returns:
int- This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters.
Example:
Str::levenshtein('kitten', 'sitting'); // 3
26. limit() Table of Contents
Limits the number of characters in a string.
Parameters:
string $value- The input string.int $limit- Maximum number of characters.string $end- Ending to append if truncated.
Returns:
string- A substring of original string with limit set on length.
Example:
Str::limit('Hello World', 5); // 'Hello...'
27. lower() Table of Contents
Converts a string to lowercase.
Parameter:
string $value- The input string.
Return:
string- The lower case string.
Example:
Str::lower('Hello'); // 'hello'
28. mask() Table of Contents
Masks portions of a string with a given character.
Parameters:
string $string- The input string.string $character- The mask character.int $start- The starting position for masking.int|null $length- The number of characters to mask.
Returns:
string- The masked string.
Example:
Str::mask('1234567890', '*', 2, 5); // '12*****890'
29. md5() Table of Contents
Generates the MD5 hash of a string.
Parameter:
string $value- The input string.
Return:
string- The MD5 has of a string.
Example:
Str::md5('hello'); // '5d41402abc4b2a76b9719d911017c592'
30. numberFormat() Table of Contents
Formats a number with grouped thousands.
Parameters:
float $number- The number to format.int $decimals- Number of decimal points.string $decimalSeparator- Decimal separator.string $thousandSeparator- Thousand separator.
Returns:
string- A formatted version of number.
Example:
Str::numberFormat(12345.678, 2); // '12,345.68'
31. padLeft() Table of Contents
Pads the left side of a string to a specified length with a given character.
Parameters:
string $value- The input string.int $length- The desired total length after padding.string $pad- The padding character.
Returns:
string- The string that is padded to the left with a given character.
Example:
Str::padLeft('hello', 8, '_'); // '___hello'
32. padRight() Table of Contents
Pads the right side of a string to a specified length with a given character.
Parameters:
string $value- The input string.int $length- The desired total length after padding.string $pad- The padding character.
Returns:
string- The string that is padded to the right with a given character.
Example:
Str::padRight('Hello', 10, '-'); // 'Hello-----'
33. pascal() Table of Contents
Converts a string to PascalCase.
Parameter:
string $value- The input string.
Return:
string- The string in PascalCase.
Example:
Str::pascal('hello world'); // 'HelloWorld'
34. plural() Table of Contents
Pluralizes a given word based on the count.
Parameters:
string $word- The word to pluralize.int $count- The number to determine singular or plural.
Returns:
string- The Pluralized string.
Example:
Str::plural('apple', 1); // 'apple'
Str::plural('apple', 2); // 'apples'
35. position() Table of Contents
Finds the position of the first occurrence of a substring.
Parameters:
string $haystack- The string to search in.string $needle- The substring to search for.
Returns:
int|false- Returns the position where the needle exists relative to the beginning of the haystack string (independent of search direction or offset). Also note that string positions start at 0, and not 1. Returns FALSE if the needle was not found.
Example:
Str::position('Hello World', 'World'); // 6
36. random() Table of Contents
Generates a random string of specified length.
Parameter:
int $length- The desired length of the random string.
Returns:
string- A random string of a specified length.
Example:
Str::random(8); // e.g., '4f9d2c8a'
37. repeat() Table of Contents
Repeats a given string a specified number of times.
Parameter:
string $value- The input string.int $times- Number of times to repeat.
Returns:
string- The repeated string.
Example:
Str::repeat('abc', 3); // 'abcabcabc'
38. replace() Table of Contents
Replaces all occurrences of the search string(s) with the given replacement(s) in the subject string.
Parameters:
string|string[]- $search The value(s) being searched for.string|string[]- $replace The replacement value(s).string $subject- The string being searched and replaced on.
Returns:
string- A string with the replaced values.
Example:
Str::replace('apple', 'orange', 'apple pie');
// 'orange pie'
Str::replace(['a', 'e'], ['A', 'E'], 'banana');
// 'bAnAnA'
39. replaceArray() Table of Contents
Sequentially replaces placeholders with values from an array.
Parameters:
string $search- The placeholder string to replace.array $replace- Array of replacement values.string $subject- The string to perform replacements on.
Returns:
string- The placeholders replaced with values from an array.
Example:
Str::replaceArray('?', ['one', 'two'], '? ?'); // 'one two'
40. replaceFirst() Table of Contents
Replaces the first occurrence of a substring.
Parameters:
string $search- The substring to find.string $replace- The substring to replace with.string $subject- The string to perform replacement on.
Returns:
string- The updated string.
Example:
Str::replaceFirst('cat', 'dog', 'cat cat'); // 'cat dog'
41. replaceLast() Table of Contents
Replaces the last occurrence of a substring.
Parameters:
string $search- The substring to find.string $replace- The substring to replace with.string $subject- The string to perform replacement on.
Returns:
string- The updated string.
Example:
Str::replaceLast('apple', 'orange', 'apple pie, apple pie');
// 'apple pie, orange pie'
42. replaceMultiple() Table of Contents
Replaces multiple substrings simultaneously.
Parameters:
array $replacements- Associative array of replacements [search => replace].string $subject- The string to perform replacements on.
Returns:
string- The updated string.
Example:
Str::replaceMultiple(['cat' => 'dog', 'blue' => 'red'], 'cat and dog');
// 'cat dog'
43. reverse() Table of Contents
Reverses the given string.
Parameter:
string $value- The input string.
Returns:
string- The reversed string.
Example:
Str::reverse('hello'); // 'olleh'
44. sha1() Table of Contents
Returns the SHA1 hash of a string.
Parameter:
string $value- The input string.
Returns:
string- The SHA1 hash of a string.
Example:
Str::sha1('hello'); // 'f7ff9e8b7bb2e09b70935d20b8a76a62cbd30d2f'
45. shuffle() Table of Contents
Randomly shuffles the characters in a string.
Parameter:
string $value- The input string.
Returns:
string- The shuffled string.
Example:
Str::shuffle('hello'); // e.g., 'eholl'
46. similarity() Table of Contents
Calculates similarity percentage between two strings.
Parameters:
string $string1- The first string.string $string2- The second string.
Returns:
int- The number of matching chars in both strings.
Example:
Str::similarity('hello', 'hallo'); // e.g., 80
47. snake() Table of Contents
Converts a string to snake_case.
Parameters:
string $value- The input string.string $delimiter- The delimiter used for snake casing.
Returns:
string- The string in snake case. Example:Str::snake('Hello World'); // 'hello_world'
48. slug() Table of Contents
Creates a URL-friendly slug from a given string.
Parameter:
string $value- The input string.string $separator- The separator used in the slug.
Returns:
string- The string represented as a URL-friendly slug.
Example:
Str::slug('Hello World!'); // 'hello-world'
49. squish() Table of Contents
Removes excessive whitespace from a string.
Parameter:
string $value- The input string.
Returns:
string- The modified string.
Example:
Str::squish(' Hello World '); // 'Hello World'
50. startsWith() Table of Contents
Determines if a string starts with a given substring.
Parameters:
string $haystack- The string to search within.string $needle- The substring to check for.
Returns:
bool- True if string starts with given substring, otherwise false.
Example:
Str::startsWith('Hello World', 'Hello'); // true
51. stripWhitespace() Table of Contents
Removes all whitespace from a given string.
Parameter:
string $value- The input string.
Returns:
string- The string with all whitespace removed.
Example:
Str::stripWhitespace('Hello World'); // 'HelloWorld'
52. studly() Table of Contents
Converts a string to StudlyCase (PascalCase).
Parameter:
string $value- The input string.
Returns:
string- The string in StudlyCase.
Example:
Str::studly('hello_world'); // 'HelloWorld'
53. substr() Table of Contents
Extracts a substring from a given string.
Parameter:
string $value- The input string.int $start- The starting position.int|null $length- The number of characters to extract.
Returns:
string- The portion of str specified by the start and length parameters.
Example:
Str::substr('Hello World', 0, 5); // 'Hello'
54. substrCount() Table of Contents
Counts the number of occurrences of a substring within a string.
Parameters:
string $haystack- The input string.string $needle- The substring to count.
Returns:
int- The number of occurrences of a substring.
Example:
Str::substrCount('apple pie apple', 'apple'); // 2
55. swapKeyValue() Table of Contents
Swaps keys with values in an array and returns a formatted string.
Parameter:
array $array- The input array.
Returns:
string- The swapped keys and values as a string.
Example:
Str::swapKeyValue(['a' => 1, 'b' => 2]); // '1 => a, 2 => b'
56. title() Table of Contents
Converts a string to title case.
Parameter:
string $value- The input string.
Returns:
string- The string in title case.
Example:
Str::title('hello world'); // 'Hello World'
57. toArray() Table of Contents
Splits a string into an array of characters.
Parameter:
string $value- The input string.
Returns:
array- The string as an array of characters.
Example:
Str::toArray('Hello'); // ['H', 'e', 'l', 'l', 'o']
58. ucfirst() Table of Contents
Capitalizes the first character of a string.
Parameter:
string $value- The input string.
Returns:
string- The string with first character capitalized.
Example:
Str::ucfirst('hello'); // 'Hello'
59. ucwords() Table of Contents
Capitalizes the first letter of each word in the string, preserving the original casing of other characters.
Parameter:
string $value- The input string.
Returns:
string- The modified string where first letter of each word is capitalized.
Example:
Str::ucwords('hello world'); // 'Hello World'
Str::ucwords('hELLO tHeRE frIEnd'); // 'HELLO THeRE FrIEnd'
60. upper() Table of Contents
Converts a string to uppercase.
Parameter:
string $value- The input string.
Returns:
string- The string in all upper case characters.
Example:
Str::upper('hello'); // 'HELLO'
61. uuid() Table of Contents
Generates a UUID (Universally Unique Identifier).
Returns:
string- A UUID string.
Example:
Str::uuid(); // '550e8400-e29b-41d4-a716-446655440000'
62. wordCount() Table of Contents
Counts the number of words in a string.
Parameter:
string $value- The input string.
Returns:
int- The number of words in a string.
Example:
Str::wordCount('Hello world'); // 2
63. words() Table of Contents
Limits a string to a certain number of words.
Parameters:
string $value- The input string.int $words- Number of words to limit to.string $end- Ending to append if truncated.
Returns:
string- The string with the limited number of words.
Example:
Str::words('Hello world of PHP', 2); // 'Hello world...'
64. wrap() Table of Contents
Wraps a string with a given value.
Parameter:
string $value- The input string.string $wrapWith- The wrapping string.
Returns:
string- The string wrapped with a given value.
Example:
Str::wrap('hello', '*'); // '*hello*'