class Str

String utility class.

Methods

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

Get the portion of a string after the first occurrence of a given value.

static string
ascii(string $value)

Convert a string to its ASCII representation.

static string
base64Encode(string $value)

Base64 encode a string.

static string
base64Decode(string $value)

Base64 decode a string.

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

Get the portion of a string before the first occurrence of a given value.

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

Get the substring between two given substrings.

static string
camel(string $value)

Convert a string to camelCase.

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

Split a string into chunks.

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

Compare two strings.

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

Determine if a string contains a given substring.

static int
crc32(string $value)

Calculate CRC32 hash of a string.

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

Determine if a string ends with a given substring.

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

Create excerpts around specific words within a string.

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

Ensure a string ends with a given value.

static string
headline(string $value)

Convert a string to headline case.

static bool
isAscii(string $value)

Check if the given string is pure ASCII.

static bool
isEmpty(string $value)

Determine if a string is empty.

static bool
isJson(string $value)

Determine if a string is a valid JSON.

static bool
isUuid(string $value)

Check if a string is a valid UUID.

static string
kebab(string $value)

Convert a string to kebab-case.

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

Find the position of the last occurrence of a substring.

static string
lcfirst(string $value)

Converts the first character of a string to lowercase.

static int
length(string $value)

Get the length of a string using multibyte support.

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

Calculate Levenshtein distance between two strings.

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

Limit the number of characters in a string.

static string
lower(string $value)

Convert a string to lowercase.

static string
mask(string $string, string $character = '*', int $start = 0, int|null $length = null)

Mask portions of a string with a given character.

static string
md5(string $value)

Return the MD5 hash of a string.

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

Format a number with grouped thousands.

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

Pad the left side of a string with a given character.

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

Pad the right side of a string with a given character.

static string
pascal(string $value)

Convert a string to PascalCase (StudlyCase).

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

Pluralizes a given word based on the count.

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

Find the position of the first occurrence of a substring.

static string
random(int $length = 16)

Generate a random string of a specified length.

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

Repeat a string.

static string
replace(string|array $search, string|array $replace, string $subject)

Replace all occurrences of the search string with the replacement string.

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

Replace placeholders sequentially with values from an array.

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

Replace the first occurrence of a substring.

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

Replace the last occurrence of a substring.

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

Replace multiple occurrences of different values in a string.

static string
reverse(string $value)

Reverse a given string.

static string
sha1(string $value)

Return the SHA1 hash of a string.

static string
shuffle(string $value)

Shuffle characters in a string.

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

Calculate similarity between two strings.

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

Convert a string to snake_case.

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

Convert a string to a URL-friendly slug.

static string
squish(string $value)

Remove excessive whitespace from a string.

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

Determine if a string starts with a given substring.

static string
stripWhitespace(string $value)

Strip all whitespace from a string.

static string
studly(string $value)

Convert a string to StudlyCase (PascalCase).

static string
substr(string $value, int $start, int|null $length = null)

Get a part of a string.

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

Count occurrences of a substring.

static string
swapKeyValue(array $array)

Swap keys with values in an array and return as a string.

static string
title(string $value)

Convert a string to title case.

static array
toArray(string $value)

Convert a string into an array.

static string
ucfirst(string $value)

Capitalize the first character of a string.

static string
ucwords(string $value)

Capitalize the first letter of each word.

static string
upper(string $value)

Convert a string to UPPERCASE.

static string
uuid()

Generate a UUID (Universally Unique Identifier).

static int
wordCount(string $value)

Count the number of words in a string.

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

Limit a string to a certain number of words.

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

Wrap a string with a given value.

Details

at line 22
static string after(string $subject, string $search)

Get the portion of a string after the first occurrence of a given value.

Parameters

string $subject

The input string.

string $search

The substring to search for.

Return Value

string

Portion of a string after first occurrence of a given value.

at line 36
static string ascii(string $value)

Convert a string to its ASCII representation.

Parameters

string $value

The input string.

Return Value

string

The ASCII representation of a string.

at line 47
static string base64Encode(string $value)

Base64 encode a string.

Parameters

string $value

The input string.

Return Value

string

The Base64 encoded string.

at line 58
static string base64Decode(string $value)

Base64 decode a string.

Parameters

string $value

The base64 encoded string.

Return Value

string

The decoded string.

at line 70
static string before(string $subject, string $search)

Get the portion of a string before the first occurrence of a given value.

Parameters

string $subject

The input string.

string $search

The substring to search for.

Return Value

string

The portion of a string before the first occurrence of a given value.

at line 85
static string between(string $value, string $start, string $end)

Get the substring between two given substrings.

Parameters

string $value

The input string.

string $start

The starting substring.

string $end

The ending substring.

Return Value

string

The substring between two given substrings.

at line 99
static string camel(string $value)

Convert a string to camelCase.

Parameters

string $value

The input string.

Return Value

string

The string in camelCase.

at line 111
static array chunk(string $value, int $length = 1)

Split a string into chunks.

Parameters

string $value

The input string.

int $length

The chunk length.

Return Value

array

An array consisting of a string split into chunks.

at line 123
static int compare(string $string1, string $string2)

Compare two strings.

Parameters

string $string1

The first string.

string $string2

The second string.

Return Value

int

Less 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

at line 135
static bool contains(string $haystack, string $needle)

Determine if a string contains a given substring.

Parameters

string $haystack

The string to search within.

string $needle

The substring to search for.

Return Value

bool

True if substring exists, otherwise false.

at line 146
static int crc32(string $value)

Calculate CRC32 hash of a string.

Parameters

string $value

The input string.

Return Value

int

The crc32 checksum of string as an integer.

at line 159
static bool endsWith(string $haystack, string $needle)

Determine if a string ends with a given substring.

Parameters

string $haystack

The string to check.

string $needle

The substring to check for.

Return Value

bool

True if string ends with given substring, otherwise we return false.

at line 172
static string excerpt(string $text, string $phrase, int $radius = 100)

Create excerpts around specific words within a string.

Parameters

string $text

The input string.

string $phrase

The phrase to excerpt around.

int $radius

The number of characters around the phrase.

Return Value

string

The portion of str specified by the start and length parameters.

at line 190
static string finish(string $value, string $cap)

Ensure a string ends with a given value.

Parameters

string $value

The input string.

string $cap

The ending string to append if missing.

Return Value

string

The string with the desired ending.

at line 201
static string headline(string $value)

Convert a string to headline case.

Parameters

string $value

The input string.

Return Value

string

The string in headline case.

at line 212
static bool isAscii(string $value)

Check if the given string is pure ASCII.

Parameters

string $value

The input string.

Return Value

bool

True on success or false on failure.

at line 223
static bool isEmpty(string $value)

Determine if a string is empty.

Parameters

string $value

The input string.

Return Value

bool

True if empty string, otherwise false.

at line 234
static bool isJson(string $value)

Determine if a string is a valid JSON.

Parameters

string $value

The input string.

Return Value

bool

True if valid JSON, otherwise false.

at line 246
static bool isUuid(string $value)

Check if a string is a valid UUID.

Parameters

string $value

The input string.

Return Value

bool

True if the string is a valid UUID, false otherwise.

at line 257
static string kebab(string $value)

Convert a string to kebab-case.

Parameters

string $value

The input string.

Return Value

string

The string in kebab-case.

at line 272
static int|false lastPosition(string $haystack, string $needle)

Find the position of the last occurrence of a substring.

Parameters

string $haystack

The string to search in.

string $needle

The substring to search for.

Return Value

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.

at line 283
static string lcfirst(string $value)

Converts the first character of a string to lowercase.

Parameters

string $value

The input string.

Return Value

string

The string with the first character converted to lowercase.

at line 295
static int length(string $value)

Get the length of a string using multibyte support.

Parameters

string $value

The input string.

Return Value

int

The number of characters in string str having character encoding. A multi-byte character is counted as 1.

at line 309
static int levenshtein(string $string1, string $string2)

Calculate Levenshtein distance between two strings.

Parameters

string $string1

The first string.

string $string2

The second string.

Return Value

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.

at line 322
static string limit(string $value, int $limit = 100, string $end = '...')

Limit 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.

Return Value

string

A substring of original string with limit set on length.

at line 333
static string lower(string $value)

Convert a string to lowercase.

Parameters

string $value

The input string.

Return Value

string

The lower case string.

at line 347
static string mask(string $string, string $character = '*', int $start = 0, int|null $length = null)

Mask 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.

Return Value

string

The masked string.

at line 361
static string md5(string $value)

Return the MD5 hash of a string.

Parameters

string $value

The input string.

Return Value

string

The MD5 has of a string.

at line 375
static string numberFormat(float $number, int $decimals = 0, string $decimalSeparator = '.', string $thousandSeparator = ',')

Format 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.

Return Value

string

A formatted version of number.

at line 388
static string padLeft(string $value, int $length, string $pad = ' ')

Pad the left side of a string with a given character.

Parameters

string $value

The input string.

int $length

The desired total length after padding.

string $pad

The padding character.

Return Value

string

The string that is padded to the left with a given character.

at line 401
static string padRight(string $value, int $length, string $pad = ' ')

Pad the right side of a string with a given character.

Parameters

string $value

The input string.

int $length

The desired total length after padding.

string $pad

The padding character.

Return Value

string

The string that is padded to the right with a given character.

at line 412
static string pascal(string $value)

Convert a string to PascalCase (StudlyCase).

Parameters

string $value

The input string.

Return Value

string

The string in PascalCase.

at line 424
static string plural(string $word, int $count = 2)

Pluralizes a given word based on the count.

Parameters

string $word

The word to pluralize.

int $count

The number to determine singular or plural.

Return Value

string

The Pluralized string.

at line 440
static int|false position(string $haystack, string $needle)

Find the position of the first occurrence of a substring.

Parameters

string $haystack

The string to search in.

string $needle

The substring to search for.

Return Value

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.

at line 451
static string random(int $length = 16)

Generate a random string of a specified length.

Parameters

int $length

The desired length of the random string.

Return Value

string

A random string of a specified length.

at line 463
static string repeat(string $value, int $times)

Repeat a string.

Parameters

string $value

The input string.

int $times

Number of times to repeat.

Return Value

string

The repeated string.

at line 476
static string replace(string|array $search, string|array $replace, string $subject)

Replace all occurrences of the search string with the replacement string.

Parameters

string|array $search

The value(s) being searched for.

string|array $replace

The replacement value(s).

string $subject

The string being searched and replaced on.

Return Value

string

A string with the replaced values.

at line 488
static string replaceArray(string $search, array $replace, string $subject)

Replace placeholders sequentially 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.

Return Value

string

The placeholders replaced with values from an array.

at line 505
static string replaceFirst(string $search, string $replace, string $subject)

Replace 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.

Return Value

string

The updated string.

at line 519
static string replaceLast(string $search, string $replace, string $subject)

Replace 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.

Return Value

string

The updated string.

at line 532
static string replaceMultiple(array $replacements, string $subject)

Replace multiple occurrences of different values in a string.

Parameters

array $replacements

Associative array of replacements [search => replace].

string $subject

The string to perform replacements on.

Return Value

string

The updated string.

at line 543
static string reverse(string $value)

Reverse a given string.

Parameters

string $value

The input string.

Return Value

string

The reversed string.

at line 554
static string sha1(string $value)

Return the SHA1 hash of a string.

Parameters

string $value

The input string.

Return Value

string

The SHA1 hash of a string.

at line 565
static string shuffle(string $value)

Shuffle characters in a string.

Parameters

string $value

The input string.

Return Value

string

The shuffled string.

at line 577
static int similarity(string $string1, string $string2)

Calculate similarity between two strings.

Parameters

string $string1

The first string.

string $string2

The second string.

Return Value

int

The number of matching chars in both strings.

at line 590
static string snake(string $value, string $delimiter = '_')

Convert a string to snake_case.

Parameters

string $value

The input string.

string $delimiter

The delimiter used for snake casing.

Return Value

string

The string in snake case.

at line 604
static string slug(string $title, string $separator = '-')

Convert a string to a URL-friendly slug.

Parameters

string $title

The input string.

string $separator

The separator used in the slug.

Return Value

string

The string represented as a URL-friendly slug.

at line 616
static string squish(string $value)

Remove excessive whitespace from a string.

Parameters

string $value

The input string.

Return Value

string

The modified string.

at line 628
static bool startsWith(string $haystack, string $needle)

Determine if a string starts with a given substring.

Parameters

string $haystack

The string to search within.

string $needle

The substring to check for.

Return Value

bool

True if string starts with given substring, otherwise false.

at line 639
static string stripWhitespace(string $value)

Strip all whitespace from a string.

Parameters

string $value

The input string.

Return Value

string

The string with all whitespace removed.

at line 650
static string studly(string $value)

Convert a string to StudlyCase (PascalCase).

Parameters

string $value

The input string.

Return Value

string

The string in StudlyCase.

at line 663
static string substr(string $value, int $start, int|null $length = null)

Get a part of a string.

Parameters

string $value

The input string.

int $start

The starting position.

int|null $length

The number of characters to extract.

Return Value

string

The portion of str specified by the start and length parameters.

at line 675
static int substrCount(string $haystack, string $needle)

Count occurrences of a substring.

Parameters

string $haystack

The input string.

string $needle

The substring to count.

Return Value

int

The number of occurrences of a substring.

at line 686
static string swapKeyValue(array $array)

Swap keys with values in an array and return as a string.

Parameters

array $array

The input array.

Return Value

string

The swapped keys and values as a string.

at line 697
static string title(string $value)

Convert a string to title case.

Parameters

string $value

The input string.

Return Value

string

The string in title case.

at line 708
static array toArray(string $value)

Convert a string into an array.

Parameters

string $value

The input string.

Return Value

array

The string as an array of characters.

at line 719
static string ucfirst(string $value)

Capitalize the first character of a string.

Parameters

string $value

The input string.

Return Value

string

The string with first character capitalized.

at line 730
static string ucwords(string $value)

Capitalize the first letter of each word.

Parameters

string $value

The input string.

Return Value

string

The modified string where first letter of each word is capitalized.

at line 741
static string upper(string $value)

Convert a string to UPPERCASE.

Parameters

string $value

The input string.

Return Value

string

The string in all upper case characters.

at line 751
static string uuid()

Generate a UUID (Universally Unique Identifier).

Return Value

string

A UUID string.

at line 762
static int wordCount(string $value)

Count the number of words in a string.

Parameters

string $value

The input string.

Return Value

int

The number of words in a string.

at line 775
static string words(string $value, int $words = 10, string $end = '...')

Limit 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.

Return Value

string

The string with the limited number of words.

at line 788
static string wrap(string $value, string $wrapWith)

Wrap a string with a given value.

Parameters

string $value

The input string.

string $wrapWith

The wrapping string.

Return Value

string

The string wrapped with a given value.