Documentation

Str
in package

The String class provides a set of handy methods for string handling and manipulation.

Table of Contents

$ascii  : array<string|int, mixed>
Ascii translation table
$defaults  : array<string|int, mixed>
Default settings for class methods
$language  : array<string|int, mixed>
Language translation table
accepted()  : array<string|int, mixed>
Parse accepted values and their quality from an accept string like an Accept or Accept-Language header
after()  : string
Returns the rest of the string after the given substring or character
afterStart()  : string
Removes the given substring or character only from the start of the string
ascii()  : string
Convert a string to 7-bit ASCII.
before()  : string
Returns the beginning of a string before the given substring or character
beforeEnd()  : string
Removes the given substring or character only from the end of the string
between()  : string
Returns everything between two strings from the first occurrence of a given string
camel()  : string
Converts a string to camel case
contains()  : bool
Checks if a str contains another string
convert()  : string
Converts a string to a different encoding
encode()  : string
Encode a string (used for email addresses)
encoding()  : string
Tries to detect the string encoding
endsWith()  : bool
Checks if a string ends with the passed needle
excerpt()  : string
Creates an excerpt of a string It removes all html tags first and then cuts the string according to the specified number of chars.
float()  : string
Convert the value to a float with a decimal point, no matter what the locale setting is
from()  : string
Returns the rest of the string starting from the given character
increment()  : string
Adds `-1` to a string or increments the ending number to allow `-2`, `-3`, etc.
kebab()  : string
Convert a string to kebab case.
length()  : int
A UTF-8 safe version of strlen()
lower()  : string
A UTF-8 safe version of strtolower()
ltrim()  : string
Safe ltrim alternative
pool()  : string|array<string|int, mixed>
Get a character pool with various possible combinations
position()  : int|bool
Returns the position of a needle in a string if it can be found
query()  : string|null
Runs a string query.
random()  : string
Generates a random string that may be used for cryptographic purposes
replace()  : string|array<string|int, mixed>
Replaces all or some occurrences of the search string with the replacement string Extension of the str_replace() function in PHP with an additional $limit parameter
replacements()  : array<string|int, mixed>
Generates a replacement array out of dynamic input data Used for Str::replace()
replaceReplacements()  : string
Takes a replacement array and processes the replacements Used for Str::replace()
rtrim()  : string
Safe rtrim alternative
short()  : string
Shortens a string and adds an ellipsis if the string is too long
similarity()  : array<string|int, mixed>
Calculates the similarity between two strings with multibyte support
slug()  : string
Convert a string to a safe version to be used in a URL
snake()  : string
Convert a string to snake case.
split()  : array<string|int, mixed>
Better alternative for explode() It takes care of removing empty values and it has a built-in way to skip values which are too short.
startsWith()  : bool
Checks if a string starts with the passed needle
studly()  : string
Converts a string to studly caps case
substr()  : string
A UTF-8 safe version of substr()
template()  : string
Replaces placeholders in string with values from the data array
toBytes()  : int
Converts a filesize string with shortcuts like M, G or K to an integer value
toType()  : mixed
Convert the string to the given type
trim()  : string
Safe trim alternative
ucfirst()  : string
A UTF-8 safe version of ucfirst()
ucwords()  : string
A UTF-8 safe version of ucwords()
until()  : string
Returns the beginning of a string until the given character
upper()  : string
A UTF-8 safe version of strotoupper()
uuid()  : string
Creates a compliant v4 UUID Taken from: https://github.com/symfony/polyfill
widont()  : string
The widont function makes sure that there are no typographical widows at the end of a paragraph – that's a single word in the last line
wrap()  : string
Wraps the string with the given string(s)

Properties

$ascii

Ascii translation table

public static array<string|int, mixed> $ascii = ['/°|₀/' => '0', '/¹|₁/' => '1', '/²|₂/' => '2', '/³|₃/' => '3', '/⁴|₄/' => '4', '/⁵|₅/' => '5', '/⁶|₆/' => '6', '/⁷|₇/' => '7', '/⁸|₈/' => '8', '/⁹|₉/' => '9', '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ|Ä|A/' => 'A', '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|æ|ǽ|ä|a|а/' => 'a', '/Б/' => 'B', '/б/' => 'b', '/Ç|Ć|Ĉ|Ċ|Č|Ц/' => 'C', '/ç|ć|ĉ|ċ|č|ц/' => 'c', '/Ð|Ď|Đ/' => 'Dj', '/ð|ď|đ/' => 'dj', '/Д/' => 'D', '/д/' => 'd', '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Е|Ё|Э/' => 'E', '/è|é|ê|ë|ē|ĕ|ė|ę|ě|е|ё|э/' => 'e', '/Ф/' => 'F', '/ƒ|ф/' => 'f', '/Ĝ|Ğ|Ġ|Ģ|Г/' => 'G', '/ĝ|ğ|ġ|ģ|г/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' => 'OE', '/Ч/' => 'Ch', '/ч/' => 'ch', '/Ю/' => 'Ju', '/ю/' => 'ju', '/Я/' => 'Ja', '/я/' => 'ja', '/Ш/' => 'Sh', '/ш/' => 'sh', '/Щ/' => 'Shch', '/щ/' => 'shch', '/Ж/' => 'Zh', '/ж/' => 'zh']

$defaults

Default settings for class methods

public static array<string|int, mixed> $defaults = ['slug' => ['separator' => '-', 'allowed' => 'a-z0-9']]

$language

Language translation table

public static array<string|int, mixed> $language = []

Methods

accepted()

Parse accepted values and their quality from an accept string like an Accept or Accept-Language header

public static accepted(string $input) : array<string|int, mixed>
Parameters
$input : string
Return values
array<string|int, mixed> —

after()

Returns the rest of the string after the given substring or character

public static after(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Return values
string —

afterStart()

Removes the given substring or character only from the start of the string

public static afterStart(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Tags
since
3.7.0
Return values
string —

ascii()

Convert a string to 7-bit ASCII.

public static ascii(string $string) : string
Parameters
$string : string
Return values
string —

before()

Returns the beginning of a string before the given substring or character

public static before(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Return values
string —

beforeEnd()

Removes the given substring or character only from the end of the string

public static beforeEnd(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Tags
since
3.7.0
Return values
string —

between()

Returns everything between two strings from the first occurrence of a given string

public static between([string $string = null ], string $start, string $end) : string
Parameters
$string : string = null
$start : string
$end : string
Return values
string —

camel()

Converts a string to camel case

public static camel([string $value = null ]) : string
Parameters
$value : string = null

The string to convert

Return values
string —

contains()

Checks if a str contains another string

public static contains([string $string = null ], string $needle[, bool $caseInsensitive = false ]) : bool
Parameters
$string : string = null
$needle : string
$caseInsensitive : bool = false
Return values
bool —

convert()

Converts a string to a different encoding

public static convert(string $string, string $targetEncoding[, string $sourceEncoding = null ]) : string
Parameters
$string : string
$targetEncoding : string
$sourceEncoding : string = null

(optional)

Return values
string —

encode()

Encode a string (used for email addresses)

public static encode(string $string) : string
Parameters
$string : string
Return values
string —

encoding()

Tries to detect the string encoding

public static encoding(string $string) : string
Parameters
$string : string
Return values
string —

endsWith()

Checks if a string ends with the passed needle

public static endsWith([string $string = null ], string $needle[, bool $caseInsensitive = false ]) : bool
Parameters
$string : string = null
$needle : string
$caseInsensitive : bool = false
Return values
bool —

excerpt()

Creates an excerpt of a string It removes all html tags first and then cuts the string according to the specified number of chars.

public static excerpt(string $string[, int $chars = 140 ][, bool $strip = true ][, string $rep = ' …' ]) : string
Parameters
$string : string

The string to be shortened

$chars : int = 140

The final number of characters the string should have

$strip : bool = true

True: remove the HTML tags from the string first

$rep : string = ' …'

The element, which should be added if the string is too long. Ellipsis is the default.

Return values
string —

The shortened string

float()

Convert the value to a float with a decimal point, no matter what the locale setting is

public static float(string|int|float $value) : string
Parameters
$value : string|int|float
Return values
string —

from()

Returns the rest of the string starting from the given character

public static from(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Return values
string —

increment()

Adds `-1` to a string or increments the ending number to allow `-2`, `-3`, etc.

public static increment(string $string[, string $separator = '-' ][, int $first = 1 ]) : string
Parameters
$string : string

The string to increment

$separator : string = '-'
$first : int = 1

Starting number

Tags
since
3.7.0
Return values
string —

kebab()

Convert a string to kebab case.

public static kebab([string $value = null ]) : string
Parameters
$value : string = null
Return values
string —

length()

A UTF-8 safe version of strlen()

public static length([string $string = null ]) : int
Parameters
$string : string = null
Return values
int —

lower()

A UTF-8 safe version of strtolower()

public static lower([string $string = null ]) : string
Parameters
$string : string = null
Return values
string —

ltrim()

Safe ltrim alternative

public static ltrim(string $string[, string $trim = ' ' ]) : string
Parameters
$string : string
$trim : string = ' '
Return values
string —

pool()

Get a character pool with various possible combinations

public static pool(string|array<string|int, mixed> $type[, bool $array = true ]) : string|array<string|int, mixed>
Parameters
$type : string|array<string|int, mixed>
$array : bool = true
Return values
string|array<string|int, mixed> —

position()

Returns the position of a needle in a string if it can be found

public static position([string $string = null ], string $needle[, bool $caseInsensitive = false ]) : int|bool
Parameters
$string : string = null
$needle : string
$caseInsensitive : bool = false
Return values
int|bool —

query()

Runs a string query.

public static query(string $query[, array<string|int, mixed> $data = [] ]) : string|null

Check out the Query class for more information.

Parameters
$query : string
$data : array<string|int, mixed> = []
Return values
string|null —

random()

Generates a random string that may be used for cryptographic purposes

public static random([int $length = null ][, string $type = 'alphaNum' ]) : string
Parameters
$length : int = null

The length of the random string

$type : string = 'alphaNum'

Pool type (type of allowed characters)

Return values
string —

replace()

Replaces all or some occurrences of the search string with the replacement string Extension of the str_replace() function in PHP with an additional $limit parameter

public static replace(string|array<string|int, mixed> $string, string|array<string|int, mixed> $search, string|array<string|int, mixed> $replace[, int|array<string|int, mixed> $limit = -1 ]) : string|array<string|int, mixed>
Parameters
$string : string|array<string|int, mixed>

String being replaced on (haystack); can be an array of multiple subject strings

$search : string|array<string|int, mixed>

Value being searched for (needle)

$replace : string|array<string|int, mixed>

Value to replace matches with

$limit : int|array<string|int, mixed> = -1

Maximum possible replacements for each search value; multiple limits for each search value are supported; defaults to no limit

Return values
string|array<string|int, mixed> —

String with replaced values; if $string is an array, array of strings

replacements()

Generates a replacement array out of dynamic input data Used for Str::replace()

public static replacements(string|array<string|int, mixed> $search, string|array<string|int, mixed> $replace, int|array<string|int, mixed> $limit) : array<string|int, mixed>
Parameters
$search : string|array<string|int, mixed>

Value being searched for (needle)

$replace : string|array<string|int, mixed>

Value to replace matches with

$limit : int|array<string|int, mixed>

Maximum possible replacements for each search value; multiple limits for each search value are supported; defaults to no limit

Return values
array<string|int, mixed> —

List of replacement arrays, each with a 'search', 'replace' and 'limit' attribute

replaceReplacements()

Takes a replacement array and processes the replacements Used for Str::replace()

public static replaceReplacements(string $string, array<string|int, mixed> $replacements) : string
Parameters
$string : string

String being replaced on (haystack)

$replacements : array<string|int, mixed>

Replacement array from Str::replacements()

Return values
string —

String with replaced values

rtrim()

Safe rtrim alternative

public static rtrim(string $string[, string $trim = ' ' ]) : string
Parameters
$string : string
$trim : string = ' '
Return values
string —

short()

Shortens a string and adds an ellipsis if the string is too long

public static short([string $string = null ], int $length[, string $appendix = '…' ]) : string

echo Str::short('This is a very, very, very long string', 10); // output: This is a…

echo Str::short('This is a very, very, very long string', 10, '####'); // output: This i####

Parameters
$string : string = null

The string to be shortened

$length : int

The final number of characters the string should have

$appendix : string = '…'

The element, which should be added if the string is too long. Ellipsis is the default.

Return values
string —

The shortened string

similarity()

Calculates the similarity between two strings with multibyte support

public static similarity(string $first, string $second[, bool $caseInsensitive = false ]) : array<string|int, mixed>
Parameters
$first : string
$second : string
$caseInsensitive : bool = false

If true, strings are compared case-insensitively

Tags
since
3.5.2
author

Based on the work of Antal Áron

copyright

Original Copyright (c) 2017, Antal Áron

license

https://github.com/antalaron/mb-similar-text/blob/master/LICENSE MIT License

Return values
array<string|int, mixed> —

matches: Number of matching chars in both strings percent: Similarity in percent

slug()

Convert a string to a safe version to be used in a URL

public static slug([string $string = null ][, string $separator = null ][, string $allowed = null ][, int $maxlength = 128 ]) : string
Parameters
$string : string = null

The unsafe string

$separator : string = null

To be used instead of space and other non-word characters.

$allowed : string = null

List of all allowed characters (regex)

$maxlength : int = 128

The maximum length of the slug

Return values
string —

The safe string

snake()

Convert a string to snake case.

public static snake([string $value = null ][, string $delimiter = '_' ]) : string
Parameters
$value : string = null
$delimiter : string = '_'
Return values
string —

split()

Better alternative for explode() It takes care of removing empty values and it has a built-in way to skip values which are too short.

public static split(string $string[, string $separator = ',' ][, int $length = 1 ]) : array<string|int, mixed>
Parameters
$string : string

The string to split

$separator : string = ','

The string to split by

$length : int = 1

The min length of values.

Return values
array<string|int, mixed> —

An array of found values

startsWith()

Checks if a string starts with the passed needle

public static startsWith([string $string = null ], string $needle[, bool $caseInsensitive = false ]) : bool
Parameters
$string : string = null
$needle : string
$caseInsensitive : bool = false
Return values
bool —

studly()

Converts a string to studly caps case

public static studly([string $value = null ]) : string
Parameters
$value : string = null

The string to convert

Tags
since
3.7.0
Return values
string —

substr()

A UTF-8 safe version of substr()

public static substr([string $string = null ], int $start[, int $length = null ]) : string
Parameters
$string : string = null
$start : int
$length : int = null
Return values
string —

template()

Replaces placeholders in string with values from the data array

public static template([string|null $string = null ][, array<string|int, mixed> $data = [] ][, array<string|int, mixed> $options = [] ]) : string

echo Str::template('From {{ b }} to {{ a }}', ['a' => 'there', 'b' => 'here']); // output: From here to there

Parameters
$string : string|null = null

The string with placeholders

$data : array<string|int, mixed> = []

Associative array with placeholders as keys and replacements as values. Supports query syntax.

$options : array<string|int, mixed> = []

An options array that contains:

  • fallback: if a token does not have any matches
  • callback: to be able to handle each matching result
  • start: start placeholder
  • end: end placeholder
Return values
string —

The filled-in string

toBytes()

Converts a filesize string with shortcuts like M, G or K to an integer value

public static toBytes(string $size) : int
Parameters
$size : string
Return values
int —

toType()

Convert the string to the given type

public static toType(string $string, mixed $type) : mixed
Parameters
$string : string
$type : mixed
Return values
mixed —

trim()

Safe trim alternative

public static trim(string $string[, string $trim = ' ' ]) : string
Parameters
$string : string
$trim : string = ' '
Return values
string —

ucfirst()

A UTF-8 safe version of ucfirst()

public static ucfirst([string $string = null ]) : string
Parameters
$string : string = null
Return values
string —

ucwords()

A UTF-8 safe version of ucwords()

public static ucwords([string $string = null ]) : string
Parameters
$string : string = null
Return values
string —

until()

Returns the beginning of a string until the given character

public static until(string $string, string $needle[, bool $caseInsensitive = false ]) : string
Parameters
$string : string
$needle : string
$caseInsensitive : bool = false
Return values
string —

upper()

A UTF-8 safe version of strotoupper()

public static upper([string $string = null ]) : string
Parameters
$string : string = null
Return values
string —

uuid()

Creates a compliant v4 UUID Taken from: https://github.com/symfony/polyfill

public static uuid() : string
Tags
since
3.7.0
Return values
string —

widont()

The widont function makes sure that there are no typographical widows at the end of a paragraph – that's a single word in the last line

public static widont([string $string = null ]) : string
Parameters
$string : string = null
Return values
string —

wrap()

Wraps the string with the given string(s)

public static wrap(string $string, string $before[, string|null $after = null ]) : string
Parameters
$string : string

String to wrap

$before : string

String to prepend

$after : string|null = null

String to append (if different from $before)

Tags
since
3.7.0
Return values
string —

Search results