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)

Pluralize a word.

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 21
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

at line 34
static string ascii(string $value)

Convert a string to its ASCII representation.

Parameters

string $value

The input string.

Return Value

string

at line 44
static string base64Encode(string $value)

Base64 encode a string.

Parameters

string $value

The input string.

Return Value

string

at line 54
static string base64Decode(string $value)

Base64 decode a string.

Parameters

string $value

The base64 encoded string.

Return Value

string

at line 65
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

at line 79
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

at line 92
static string camel(string $value)

Convert a string to camelCase.

Parameters

string $value

The input string.

Return Value

string

at line 103
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

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

Compare two strings.

Parameters

string $string1

The first string.

string $string2

The second string.

Return Value

int

at line 125
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

at line 135
static int crc32(string $value)

Calculate CRC32 hash of a string.

Parameters

string $value

The input string.

Return Value

int

at line 146
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

at line 158
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

at line 175
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

at line 185
static string headline(string $value)

Convert a string to headline case.

Parameters

string $value

The input string.

Return Value

string

at line 195
static bool isAscii(string $value)

Check if the given string is pure ASCII.

Parameters

string $value

The input string.

Return Value

bool

at line 205
static bool isEmpty(string $value)

Determine if a string is empty.

Parameters

string $value

The input string.

Return Value

bool

at line 215
static bool isJson(string $value)

Determine if a string is a valid JSON.

Parameters

string $value

The input string.

Return Value

bool

at line 226
static bool isUuid(string $value)

Check if a string is a valid UUID.

Parameters

string $value

The input string.

Return Value

bool

at line 236
static string kebab(string $value)

Convert a string to kebab-case.

Parameters

string $value

The input string.

Return Value

string

at line 248
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

at line 259
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 269
static int length(string $value)

Get the length of a string using multibyte support.

Parameters

string $value

The input string.

Return Value

int

at line 280
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

at line 292
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

at line 302
static string lower(string $value)

Convert a string to lowercase.

Parameters

string $value

The input string.

Return Value

string

at line 315
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

at line 328
static string md5(string $value)

Return the MD5 hash of a string.

Parameters

string $value

The input string.

Return Value

string

at line 341
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

at line 353
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

at line 365
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

at line 375
static string pascal(string $value)

Convert a string to PascalCase (StudlyCase).

Parameters

string $value

The input string.

Return Value

string

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

Pluralize a word.

Parameters

string $word

The word to pluralize.

int $count

The number to determine singular or plural.

Return Value

string

at line 399
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

at line 409
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

at line 420
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

at line 433
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

at line 444
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

at line 460
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

at line 473
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

at line 485
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

at line 495
static string reverse(string $value)

Reverse a given string.

Parameters

string $value

The input string.

Return Value

string

at line 505
static string sha1(string $value)

Return the SHA1 hash of a string.

Parameters

string $value

The input string.

Return Value

string

at line 515
static string shuffle(string $value)

Shuffle characters in a string.

Parameters

string $value

The input string.

Return Value

string

at line 526
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

at line 538
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

at line 551
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

at line 562
static string squish(string $value)

Remove excessive whitespace from a string.

Parameters

string $value

The input string.

Return Value

string

at line 573
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

at line 583
static string stripWhitespace(string $value)

Strip all whitespace from a string.

Parameters

string $value

The input string.

Return Value

string

at line 593
static string studly(string $value)

Convert a string to StudlyCase (PascalCase).

Parameters

string $value

The input string.

Return Value

string

at line 605
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

at line 616
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

at line 626
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

at line 636
static string title(string $value)

Convert a string to title case.

Parameters

string $value

The input string.

Return Value

string

at line 646
static array toArray(string $value)

Convert a string into an array.

Parameters

string $value

The input string.

Return Value

array

at line 656
static string ucfirst(string $value)

Capitalize the first character of a string.

Parameters

string $value

The input string.

Return Value

string

at line 666
static string ucwords(string $value)

Capitalize the first letter of each word.

Parameters

string $value

The input string.

Return Value

string

at line 676
static string upper(string $value)

Convert a string to UPPERCASE.

Parameters

string $value

The input string.

Return Value

string

at line 684
static string uuid()

Generate a UUID (Universally Unique Identifier).

Return Value

string

at line 694
static int wordCount(string $value)

Count the number of words in a string.

Parameters

string $value

The input string.

Return Value

int

at line 706
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

at line 718
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