class Strings

String tools library.

Constants

TrimCharacters

deprecated TRIM_CHARACTERS

Methods

static bool
checkEncoding(string $s) deprecated

No description

static string
fixEncoding(string $s)

Removes all invalid UTF-8 characters from a string.

static string
chr(int $code)

Returns a specific character in UTF-8 from code point (number in range 0x0000..D7FF or 0xE000..10FFFF).

static int
ord(string $c)

Returns a code point of specific character in UTF-8 (number in range 0x0000..D7FF or 0xE000..10FFFF).

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

No description

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

No description

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

No description

static string
substring(string $s, int $start, int|null $length = null)

Returns a part of UTF-8 string specified by starting position and length. If start is negative, the returned string will start at the start'th character from the end of string.

static string
normalize(string $s)

Removes control characters, normalizes line breaks to \n, removes leading and trailing blank lines, trims end spaces on lines, normalizes UTF-8 to the normal form of NFC.

static string
normalizeNewLines(string $s) deprecated

No description

static string
unixNewLines(string $s)

Converts line endings to \n used on Unix-like systems.

static string
platformNewLines(string $s)

Converts line endings to platform-specific, i.e. \r\n on Windows and \n elsewhere.

static string
toAscii(string $s)

Converts UTF-8 string to ASCII, ie removes diacritics etc.

static string
webalize(string $s, string|null $charlist = null, bool $lower = true)

Modifies the UTF-8 string to the form used in the URL, ie removes diacritics and replaces all characters except letters of the English alphabet and numbers with a hyphens.

static string
truncate(string $s, int $maxLen, string $append = "…")

Truncates a UTF-8 string to given maximal length, while trying not to split whole words. Only if the string is truncated, an ellipsis (or something else set with third argument) is appended to the string.

static string
indent(string $s, int $level = 1, string $chars = "\t")

Indents a multiline text from the left. Second argument sets how many indentation chars should be used, while the indent itself is the third argument (tab by default).

static string
lower(string $s)

Converts all characters of UTF-8 string to lower case.

static string
firstLower(string $s)

Converts the first character of a UTF-8 string to lower case and leaves the other characters unchanged.

static string
upper(string $s)

Converts all characters of a UTF-8 string to upper case.

static string
firstUpper(string $s)

Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.

static string
capitalize(string $s)

Converts the first character of every word of a UTF-8 string to upper case and the others to lower case.

static bool
compare(string $left, string $right, int|null $length = null)

Compares two UTF-8 strings or their parts, without taking character case into account. If length is null, whole strings are compared, if it is negative, the corresponding number of characters from the end of the strings is compared, otherwise the appropriate number of characters from the beginning is compared.

static string
findPrefix(array $strings)

Finds the common prefix of strings or returns empty string if the prefix was not found.

static int
length(string $s)

Returns number of characters (not bytes) in UTF-8 string.

static string
trim(string $s, string $charlist = self::TrimCharacters)

Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.

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

Pads a UTF-8 string to given length by prepending the $pad string to the beginning.

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

Pads UTF-8 string to given length by appending the $pad string to the end.

static string
reverse(string $s)

Reverses UTF-8 string.

static string|null
before(string $haystack, string $needle, int $nth = 1)

Returns part of $haystack before $nth occurence of $needle or returns null if the needle was not found.

static string|null
after(string $haystack, string $needle, int $nth = 1)

Returns part of $haystack after $nth occurence of $needle or returns null if the needle was not found.

static int|null
indexOf(string $haystack, string $needle, int $nth = 1)

Returns position in characters of $nth occurence of $needle in $haystack or null if the $needle was not found.

static array
split(string $subject, string $pattern, bool|int $captureOffset = false, bool $skipEmpty = false, int $limit = -1, bool $utf8 = false)

Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.

static array|null
match(string $subject, string $pattern, bool|int $captureOffset = false, int $offset = 0, bool $unmatchedAsNull = false, bool $utf8 = false)

Searches the string for the part matching the regular expression and returns an array with the found expression and individual subexpressions, or null.

static array|Generator
matchAll(string $subject, string $pattern, bool|int $captureOffset = false, int $offset = 0, bool $unmatchedAsNull = false, bool $patternOrder = false, bool $utf8 = false, bool $lazy = false)

Searches the string for all occurrences matching the regular expression and returns an array of arrays containing the found expression and each subexpression.

static string
replace(string $subject, string|array $pattern, string|callable $replacement = '', int $limit = -1, bool $captureOffset = false, bool $unmatchedAsNull = false, bool $utf8 = false)

Replaces all occurrences matching regular expression $pattern which can be string or array in the form pattern => replacement.

static 
pcre(string $func, array $args)

No description

Details

at line 33
static bool checkEncoding(string $s) deprecated

deprecated use Nette\Utils\Validator::isUnicode()

No description

Parameters

string $s

Return Value

bool

at line 42
static string fixEncoding(string $s)

Removes all invalid UTF-8 characters from a string.

Parameters

string $s

Return Value

string

at line 53
static string chr(int $code)

Returns a specific character in UTF-8 from code point (number in range 0x0000..D7FF or 0xE000..10FFFF).

Parameters

int $code

Return Value

string

Exceptions

InvalidArgumentException

at line 68
static int ord(string $c)

Returns a code point of specific character in UTF-8 (number in range 0x0000..D7FF or 0xE000..10FFFF).

Parameters

string $c

Return Value

int

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

deprecated use str_starts_with()

No description

Parameters

string $haystack
string $needle

Return Value

bool

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

deprecated use str_ends_with()

No description

Parameters

string $haystack
string $needle

Return Value

bool

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

deprecated use str_contains()

No description

Parameters

string $haystack
string $needle

Return Value

bool

at line 114
static string substring(string $s, int $start, int|null $length = null)

Returns a part of UTF-8 string specified by starting position and length. If start is negative, the returned string will start at the start'th character from the end of string.

Parameters

string $s
int $start
int|null $length

Return Value

string

at line 134
static string normalize(string $s)

Removes control characters, normalizes line breaks to \n, removes leading and trailing blank lines, trims end spaces on lines, normalizes UTF-8 to the normal form of NFC.

Parameters

string $s

Return Value

string

at line 157
static string normalizeNewLines(string $s) deprecated

deprecated use Strings::unixNewLines()

No description

Parameters

string $s

Return Value

string

at line 167
static string unixNewLines(string $s)

Converts line endings to \n used on Unix-like systems.

Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.

Parameters

string $s

Return Value

string

at line 177
static string platformNewLines(string $s)

Converts line endings to platform-specific, i.e. \r\n on Windows and \n elsewhere.

Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.

Parameters

string $s

Return Value

string

at line 186
static string toAscii(string $s)

Converts UTF-8 string to ASCII, ie removes diacritics etc.

Parameters

string $s

Return Value

string

at line 252
static string webalize(string $s, string|null $charlist = null, bool $lower = true)

Modifies the UTF-8 string to the form used in the URL, ie removes diacritics and replaces all characters except letters of the English alphabet and numbers with a hyphens.

Parameters

string $s
string|null $charlist
bool $lower

Return Value

string

at line 269
static string truncate(string $s, int $maxLen, string $append = "…")

Truncates a UTF-8 string to given maximal length, while trying not to split whole words. Only if the string is truncated, an ellipsis (or something else set with third argument) is appended to the string.

Parameters

string $s
int $maxLen
string $append

Return Value

string

at line 292
static string indent(string $s, int $level = 1, string $chars = "\t")

Indents a multiline text from the left. Second argument sets how many indentation chars should be used, while the indent itself is the third argument (tab by default).

Parameters

string $s
int $level
string $chars

Return Value

string

at line 305
static string lower(string $s)

Converts all characters of UTF-8 string to lower case.

Parameters

string $s

Return Value

string

at line 314
static string firstLower(string $s)

Converts the first character of a UTF-8 string to lower case and leaves the other characters unchanged.

Parameters

string $s

Return Value

string

at line 323
static string upper(string $s)

Converts all characters of a UTF-8 string to upper case.

Parameters

string $s

Return Value

string

at line 332
static string firstUpper(string $s)

Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.

Parameters

string $s

Return Value

string

at line 341
static string capitalize(string $s)

Converts the first character of every word of a UTF-8 string to upper case and the others to lower case.

Parameters

string $s

Return Value

string

at line 352
static bool compare(string $left, string $right, int|null $length = null)

Compares two UTF-8 strings or their parts, without taking character case into account. If length is null, whole strings are compared, if it is negative, the corresponding number of characters from the end of the strings is compared, otherwise the appropriate number of characters from the beginning is compared.

Parameters

string $left
string $right
int|null $length

Return Value

bool

at line 375
static string findPrefix(array $strings)

Finds the common prefix of strings or returns empty string if the prefix was not found.

Parameters

array $strings

Return Value

string

at line 398
static int length(string $s)

Returns number of characters (not bytes) in UTF-8 string.

That is the number of Unicode code points which may differ from the number of graphemes.

Parameters

string $s

Return Value

int

at line 411
static string trim(string $s, string $charlist = self::TrimCharacters)

Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.

Parameters

string $s
string $charlist

Return Value

string

at line 422
static string padLeft(string $s, int $length, string $pad = ' ')

Pads a UTF-8 string to given length by prepending the $pad string to the beginning.

Parameters

string $s
int $length
string $pad

Return Value

string

at line 434
static string padRight(string $s, int $length, string $pad = ' ')

Pads UTF-8 string to given length by appending the $pad string to the end.

Parameters

string $s
int $length
string $pad

Return Value

string

at line 445
static string reverse(string $s)

Reverses UTF-8 string.

Parameters

string $s

Return Value

string

at line 459
static string|null before(string $haystack, string $needle, int $nth = 1)

Returns part of $haystack before $nth occurence of $needle or returns null if the needle was not found.

Negative value means searching from the end.

Parameters

string $haystack
string $needle
int $nth

Return Value

string|null

at line 472
static string|null after(string $haystack, string $needle, int $nth = 1)

Returns part of $haystack after $nth occurence of $needle or returns null if the needle was not found.

Negative value means searching from the end.

Parameters

string $haystack
string $needle
int $nth

Return Value

string|null

at line 485
static int|null indexOf(string $haystack, string $needle, int $nth = 1)

Returns position in characters of $nth occurence of $needle in $haystack or null if the $needle was not found.

Negative value of $nth means searching from the end.

Parameters

string $haystack
string $needle
int $nth

Return Value

int|null

at line 531
static array split(string $subject, string $pattern, bool|int $captureOffset = false, bool $skipEmpty = false, int $limit = -1, bool $utf8 = false)

Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.

Parameters

string $subject
string $pattern
bool|int $captureOffset
bool $skipEmpty
int $limit
bool $utf8

Return Value

array

at line 557
static array|null match(string $subject, string $pattern, bool|int $captureOffset = false, int $offset = 0, bool $unmatchedAsNull = false, bool $utf8 = false)

Searches the string for the part matching the regular expression and returns an array with the found expression and individual subexpressions, or null.

Parameters

string $subject
string $pattern
bool|int $captureOffset
int $offset
bool $unmatchedAsNull
bool $utf8

Return Value

array|null

at line 593
static array|Generator matchAll(string $subject, string $pattern, bool|int $captureOffset = false, int $offset = 0, bool $unmatchedAsNull = false, bool $patternOrder = false, bool $utf8 = false, bool $lazy = false)

Searches the string for all occurrences matching the regular expression and returns an array of arrays containing the found expression and each subexpression.

Parameters

string $subject
string $pattern
bool|int $captureOffset
int $offset
bool $unmatchedAsNull
bool $patternOrder
bool $utf8
bool $lazy

Return Value

array|Generator

at line 651
static string replace(string $subject, string|array $pattern, string|callable $replacement = '', int $limit = -1, bool $captureOffset = false, bool $unmatchedAsNull = false, bool $utf8 = false)

Replaces all occurrences matching regular expression $pattern which can be string or array in the form pattern => replacement.

Parameters

string $subject
string|array $pattern
string|callable $replacement
int $limit
bool $captureOffset
bool $unmatchedAsNull
bool $utf8

Return Value

string

at line 711
static pcre(string $func, array $args)

internal  
 

No description

Parameters

string $func
array $args

Traits

Static class.