Strings
in package
uses
StaticClass
String tools library.
Table of Contents
Constants
- TRIM_CHARACTERS = self::TrimCharacters
- TrimCharacters = " \t\n\r\x00\v "
Methods
- __callStatic() : mixed
- Call to undefined static method.
- after() : string|null
- Returns part of $haystack after $nth occurence of $needle or returns null if the needle was not found.
- before() : string|null
- Returns part of $haystack before $nth occurence of $needle or returns null if the needle was not found.
- capitalize() : string
- Converts the first character of every word of a UTF-8 string to upper case and the others to lower case.
- checkEncoding() : bool
- chr() : string
- Returns a specific character in UTF-8 from code point (number in range 0x0000..D7FF or 0xE000..10FFFF).
- compare() : bool
- 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.
- contains() : bool
- endsWith() : bool
- findPrefix() : string
- Finds the common prefix of strings or returns empty string if the prefix was not found.
- firstLower() : string
- Converts the first character of a UTF-8 string to lower case and leaves the other characters unchanged.
- firstUpper() : string
- Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.
- fixEncoding() : string
- Removes all invalid UTF-8 characters from a string.
- indent() : string
- 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).
- indexOf() : int|null
- Returns position in characters of $nth occurence of $needle in $haystack or null if the $needle was not found.
- length() : int
- Returns number of characters (not bytes) in UTF-8 string.
- lower() : string
- Converts all characters of UTF-8 string to lower case.
- match() : array<string|int, mixed>|null
- Searches the string for the part matching the regular expression and returns an array with the found expression and individual subexpressions, or `null`.
-
matchAll()
: Generator
: array[]) - Searches the string for all occurrences matching the regular expression and returns an array of arrays containing the found expression and each subexpression.
- normalize() : string
- 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.
- normalizeNewLines() : string
- ord() : int
- Returns a code point of specific character in UTF-8 (number in range 0x0000..D7FF or 0xE000..10FFFF).
- padLeft() : string
- Pads a UTF-8 string to given length by prepending the $pad string to the beginning.
- padRight() : string
- Pads UTF-8 string to given length by appending the $pad string to the end.
- platformNewLines() : string
- Converts line endings to platform-specific, i.e. \r\n on Windows and \n elsewhere.
- replace() : string
- Replaces all occurrences matching regular expression $pattern which can be string or array in the form `pattern => replacement`.
- reverse() : string
- Reverses UTF-8 string.
- split() : array<string|int, mixed>
- Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.
- startsWith() : bool
- substring() : string
- 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.
- toAscii() : string
- Converts UTF-8 string to ASCII, ie removes diacritics etc.
- trim() : string
- Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.
- truncate() : string
- 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.
- unixNewLines() : string
- Converts line endings to \n used on Unix-like systems.
- upper() : string
- Converts all characters of a UTF-8 string to upper case.
- webalize() : string
- 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.
- __construct() : mixed
- Class is static and cannot be instantiated.
- bytesToChars() : array<string|int, mixed>
- pos() : int|null
- Returns position in characters of $nth occurence of $needle in $haystack or null if the needle was not found.
Constants
TRIM_CHARACTERS
use Strings::TrimCharacters
public
mixed
TRIM_CHARACTERS
= self::TrimCharacters
TrimCharacters
public
mixed
TrimCharacters
= " \t\n\r\x00\v "
Methods
__callStatic()
Call to undefined static method.
public
static __callStatic(string $name, array<string|int, mixed> $args) : mixed
Parameters
- $name : string
- $args : array<string|int, mixed>
Tags
after()
Returns part of $haystack after $nth occurence of $needle or returns null if the needle was not found.
public
static after(string $haystack, string $needle[, int $nth = 1 ]) : string|null
Negative value means searching from the end.
Parameters
- $haystack : string
- $needle : string
- $nth : int = 1
Return values
string|nullbefore()
Returns part of $haystack before $nth occurence of $needle or returns null if the needle was not found.
public
static before(string $haystack, string $needle[, int $nth = 1 ]) : string|null
Negative value means searching from the end.
Parameters
- $haystack : string
- $needle : string
- $nth : int = 1
Return values
string|nullcapitalize()
Converts the first character of every word of a UTF-8 string to upper case and the others to lower case.
public
static capitalize(string $s) : string
Parameters
- $s : string
Return values
stringcheckEncoding()
public
static checkEncoding(string $s) : bool
use Nette\Utils\Validators::isUnicode()
Parameters
- $s : string
Return values
boolchr()
Returns a specific character in UTF-8 from code point (number in range 0x0000..D7FF or 0xE000..10FFFF).
public
static chr(int $code) : string
Parameters
- $code : int
Tags
Return values
stringcompare()
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.
public
static compare(string $left, string $right[, int|null $length = null ]) : bool
Parameters
- $left : string
- $right : string
- $length : int|null = null
Return values
boolcontains()
public
static contains(string $haystack, string $needle) : bool
use str_contains()
Parameters
- $haystack : string
- $needle : string
Return values
boolendsWith()
public
static endsWith(string $haystack, string $needle) : bool
use str_ends_with()
Parameters
- $haystack : string
- $needle : string
Return values
boolfindPrefix()
Finds the common prefix of strings or returns empty string if the prefix was not found.
public
static findPrefix(array<string|int, string> $strings) : string
Parameters
- $strings : array<string|int, string>
Return values
stringfirstLower()
Converts the first character of a UTF-8 string to lower case and leaves the other characters unchanged.
public
static firstLower(string $s) : string
Parameters
- $s : string
Return values
stringfirstUpper()
Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.
public
static firstUpper(string $s) : string
Parameters
- $s : string
Return values
stringfixEncoding()
Removes all invalid UTF-8 characters from a string.
public
static fixEncoding(string $s) : string
Parameters
- $s : string
Return values
stringindent()
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).
public
static indent(string $s[, int $level = 1 ][, string $chars = "\t" ]) : string
Parameters
- $s : string
- $level : int = 1
- $chars : string = "\t"
Return values
stringindexOf()
Returns position in characters of $nth occurence of $needle in $haystack or null if the $needle was not found.
public
static indexOf(string $haystack, string $needle[, int $nth = 1 ]) : int|null
Negative value of $nth means searching from the end.
Parameters
- $haystack : string
- $needle : string
- $nth : int = 1
Return values
int|nulllength()
Returns number of characters (not bytes) in UTF-8 string.
public
static length(string $s) : int
That is the number of Unicode code points which may differ from the number of graphemes.
Parameters
- $s : string
Return values
intlower()
Converts all characters of UTF-8 string to lower case.
public
static lower(string $s) : string
Parameters
- $s : string
Return values
stringmatch()
Searches the string for the part matching the regular expression and returns an array with the found expression and individual subexpressions, or `null`.
public
static match(string $subject, string $pattern[, bool|int $captureOffset = false ][, int $offset = 0 ][, bool $unmatchedAsNull = false ][, bool $utf8 = false ]) : array<string|int, mixed>|null
Parameters
- $subject : string
- $pattern : string
- $captureOffset : bool|int = false
- $offset : int = 0
- $unmatchedAsNull : bool = false
- $utf8 : bool = false
Return values
array<string|int, mixed>|nullmatchAll()
Searches the string for all occurrences matching the regular expression and returns an array of arrays containing the found expression and each subexpression.
public
static matchAll(string $subject, string $pattern[, bool|int $captureOffset = false ][, int $offset = 0 ][, bool $unmatchedAsNull = false ][, bool $patternOrder = false ][, bool $utf8 = false ][, bool $lazy = false ]) : Generator : array[])
Parameters
- $subject : string
- $pattern : string
- $captureOffset : bool|int = false
- $offset : int = 0
- $unmatchedAsNull : bool = false
- $patternOrder : bool = false
- $utf8 : bool = false
- $lazy : bool = false
Return values
Generatornormalize()
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.
public
static normalize(string $s) : string
Parameters
- $s : string
Return values
stringnormalizeNewLines()
public
static normalizeNewLines(string $s) : string
use Strings::unixNewLines()
Parameters
- $s : string
Return values
stringord()
Returns a code point of specific character in UTF-8 (number in range 0x0000..D7FF or 0xE000..10FFFF).
public
static ord(string $c) : int
Parameters
- $c : string
Return values
intpadLeft()
Pads a UTF-8 string to given length by prepending the $pad string to the beginning.
public
static padLeft(string $s, int $length[, non-empty-string $pad = ' ' ]) : string
Parameters
- $s : string
- $length : int
- $pad : non-empty-string = ' '
Return values
stringpadRight()
Pads UTF-8 string to given length by appending the $pad string to the end.
public
static padRight(string $s, int $length[, non-empty-string $pad = ' ' ]) : string
Parameters
- $s : string
- $length : int
- $pad : non-empty-string = ' '
Return values
stringplatformNewLines()
Converts line endings to platform-specific, i.e. \r\n on Windows and \n elsewhere.
public
static platformNewLines(string $s) : string
Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.
Parameters
- $s : string
Return values
stringreplace()
Replaces all occurrences matching regular expression $pattern which can be string or array in the form `pattern => replacement`.
public
static replace(string $subject, string|array<string|int, mixed> $pattern[, string|callable $replacement = '' ][, int $limit = -1 ][, bool $captureOffset = false ][, bool $unmatchedAsNull = false ][, bool $utf8 = false ]) : string
Parameters
- $subject : string
- $pattern : string|array<string|int, mixed>
- $replacement : string|callable = ''
- $limit : int = -1
- $captureOffset : bool = false
- $unmatchedAsNull : bool = false
- $utf8 : bool = false
Return values
stringreverse()
Reverses UTF-8 string.
public
static reverse(string $s) : string
Parameters
- $s : string
Return values
stringsplit()
Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.
public
static split(string $subject, string $pattern[, bool|int $captureOffset = false ][, bool $skipEmpty = false ][, int $limit = -1 ][, bool $utf8 = false ]) : array<string|int, mixed>
Parameters
- $subject : string
- $pattern : string
- $captureOffset : bool|int = false
- $skipEmpty : bool = false
- $limit : int = -1
- $utf8 : bool = false
Return values
array<string|int, mixed>startsWith()
public
static startsWith(string $haystack, string $needle) : bool
use str_starts_with()
Parameters
- $haystack : string
- $needle : string
Return values
boolsubstring()
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.
public
static substring(string $s, int $start[, int|null $length = null ]) : string
Parameters
- $s : string
- $start : int
- $length : int|null = null
Return values
stringtoAscii()
Converts UTF-8 string to ASCII, ie removes diacritics etc.
public
static toAscii(string $s) : string
Parameters
- $s : string
Return values
stringtrim()
Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string.
public
static trim(string $s[, string $charlist = self::TrimCharacters ]) : string
Parameters
- $s : string
- $charlist : string = self::TrimCharacters
Return values
stringtruncate()
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.
public
static truncate(string $s, int $maxLen[, string $append = "…" ]) : string
Parameters
- $s : string
- $maxLen : int
- $append : string = "…"
Return values
stringunixNewLines()
Converts line endings to \n used on Unix-like systems.
public
static unixNewLines(string $s) : string
Line endings are: \n, \r, \r\n, U+2028 line separator, U+2029 paragraph separator.
Parameters
- $s : string
Return values
stringupper()
Converts all characters of a UTF-8 string to upper case.
public
static upper(string $s) : string
Parameters
- $s : string
Return values
stringwebalize()
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.
public
static webalize(string $s[, string|null $charlist = null ][, bool $lower = true ]) : string
Parameters
- $s : string
- $charlist : string|null = null
- $lower : bool = true
Return values
string__construct()
Class is static and cannot be instantiated.
private
__construct() : mixed
bytesToChars()
private
static bytesToChars(string $s, array<string|int, mixed> $groups) : array<string|int, mixed>
Parameters
- $s : string
- $groups : array<string|int, mixed>
Return values
array<string|int, mixed>pos()
Returns position in characters of $nth occurence of $needle in $haystack or null if the needle was not found.
private
static pos(string $haystack, string $needle[, int $nth = 1 ]) : int|null
Parameters
- $haystack : string
- $needle : string
- $nth : int = 1