Methods |
public
static
|
coalesce(?string ...$strings): ?string
Get the first string that is not null or empty, or return the last value
Get the first string that is not null or empty, or return the last value
|
#
|
public
static
|
lower(string $string): string
Convert an ASCII string to lowercase
Convert an ASCII string to lowercase
|
#
|
public
static
|
upper(string $string): string
Convert an ASCII string to uppercase
Convert an ASCII string to uppercase
|
#
|
public
static
|
upperFirst(string $string): string
Make the first character in an ASCII string uppercase
Make the first character in an ASCII string uppercase
|
#
|
public
static
|
matchCase(string $string, string $match): string
Match an ASCII string's case to another string
Match an ASCII string's case to another string
|
#
|
public
static
|
startsWith(string $haystack, iterable<string>|string $needles, bool $ignoreCase = false): bool
Check if a string starts with a given substring
Check if a string starts with a given substring
|
#
|
public
static
|
endsWith(string $haystack, iterable<string>|string $needles, bool $ignoreCase = false): bool
Check if a string ends with a given substring
Check if a string ends with a given substring
|
#
|
public
static
|
isAscii(string $string): bool
Check if every character in a string has a codepoint between 0 and 127
Check if every character in a string has a codepoint between 0 and 127
|
#
|
public
static
|
escapeMarkdown(string $string): string
Escape special characters in a string for use in Markdown
Escape special characters in a string for use in Markdown
|
#
|
public
static
|
normalise(string $string): string
Normalise a string for comparison
Normalise a string for comparison
The return value of this method is not covered by the Salient toolkit's
backward compatibility promise.
|
#
|
public
static
|
ellipsize(string $value, int $length): string
Replace the end of a string with an ellipsis ("...") if its length
exceeds a limit
Replace the end of a string with an ellipsis ("...") if its length
exceeds a limit
|
#
|
public
static
|
setEol(string $string, string $eol = "\n"): string
Apply an end-of-line sequence to a string
Apply an end-of-line sequence to a string
|
#
|
public
static
|
trimNativeEol(string $string): string
Remove native end-of-line sequences from the end of a string
Remove native end-of-line sequences from the end of a string
|
#
|
public
static
|
eolToNative(string $string): string
Replace newline characters in a string with the native end-of-line
sequence
Replace newline characters in a string with the native end-of-line
sequence
|
#
|
public
static
|
eolFromNative(string $string): string
Replace native end-of-line sequences in a string with the newline
character
Replace native end-of-line sequences in a string with the newline
character
|
#
|
public
static
|
snake(string $string, string $preserve = ''): string
Convert words in an arbitrarily capitalised string to snake_case,
optionally preserving non-word characters
Convert words in an arbitrarily capitalised string to snake_case,
optionally preserving non-word characters
|
#
|
public
static
|
kebab(string $string, string $preserve = ''): string
Convert words in an arbitrarily capitalised string to kebab-case,
optionally preserving non-word characters
Convert words in an arbitrarily capitalised string to kebab-case,
optionally preserving non-word characters
|
#
|
public
static
|
camel(string $string, string $preserve = ''): string
Convert words in an arbitrarily capitalised string to camelCase,
optionally preserving non-word characters
Convert words in an arbitrarily capitalised string to camelCase,
optionally preserving non-word characters
|
#
|
public
static
|
pascal(string $string, string $preserve = ''): string
Convert words in an arbitrarily capitalised string to PascalCase,
optionally preserving non-word characters
Convert words in an arbitrarily capitalised string to PascalCase,
optionally preserving non-word characters
|
#
|
public
static
|
words(
string $string,
string $separator = ' ',
string $preserve = '',
(Closure(string): string)|null $callback = null,
): string
Get words from an arbitrarily capitalised string and delimit them with a
separator, optionally preserving non-word…
Get words from an arbitrarily capitalised string and delimit them with a
separator, optionally preserving non-word characters and applying a
callback to each word
A word consists of one or more letters of the same case, or one uppercase
letter followed by zero or more lowercase letters. Numbers are treated as
lowercase letters except that two or more uppercase letters form one word
with any subsequent numbers.
Parameters
$preserve |
Non-alphanumeric characters to preserve.
|
|
#
|
public
static
|
expandTabs(string $string, int $tabSize = 8, int $column = 1): string
Expand tabs in a string to spaces
Expand tabs in a string to spaces
Parameters
$column |
The starting column (1-based) of $text .
|
|
#
|
public
static
|
expandLeadingTabs(string $string, int $tabSize = 8, bool $preserveLine1 = false, int $column = 1): string
Expand leading tabs in a string to spaces
Expand leading tabs in a string to spaces
Parameters
$preserveLine1 |
If true , tabs in the first line of $text
are not expanded.
|
$column |
The starting column (1-based) of $text .
|
|
#
|
public
static
|
toStream(string $string): resource
Copy a string to a temporary stream
Copy a string to a temporary stream
|
#
|
public
static
|
split(
non-empty-string $separator,
string $string,
int|null $limit = null,
bool $removeEmpty = true,
string|null $characters = null,
): ($removeEmpty is true ? list<string> : non-empty-list<string>)
Split a string by a string, trim substrings and remove any empty strings
Split a string by a string, trim substrings and remove any empty strings
Parameters
$limit |
The maximum number of substrings to return. If
given, empty strings are not removed.
|
$characters |
Characters to trim, null (the default)
to trim whitespace, or an empty string to trim nothing.
|
|
#
|
public
static
|
splitDelimited(
non-empty-string $separator,
string $string,
bool $removeEmpty = true,
string|null $characters = null,
int-mask-of<Str::PRESERVE_*> $flags = Str::PRESERVE_DOUBLE_QUOTED,
): ($removeEmpty is true ? list<string> : non-empty-list<string>)
Split a string by a string without splitting bracket-delimited or
double-quoted substrings, trim substrings and remove…
Split a string by a string without splitting bracket-delimited or
double-quoted substrings, trim substrings and remove any empty strings
Parameters
$characters |
Characters to trim, null (the default)
to trim whitespace, or an empty string to trim nothing.
|
|
#
|
public
static
|
wrap(string $string, int|array{int, int} $width = 75, string $break = "\n", bool $cutLongWords = false): string
Wrap a string to a given number of characters, optionally varying the
width of the first line
Wrap a string to a given number of characters, optionally varying the
width of the first line
Parameters
$width |
The number of characters at which the
string will be wrapped, or [ <first_line_width>, <width> ] .
|
|
#
|
public
static
|
unwrap(
string $string,
string $break = "\n",
bool $ignoreEscapes = true,
bool $trimLines = false,
bool $collapseBlankLines = false,
): string
Undo wordwrap(), preserving Markdown-style paragraphs and lists
Undo wordwrap(), preserving Markdown-style paragraphs and lists
Non-consecutive line breaks are converted to spaces except before:
- four or more spaces
- one or more tabs
- Markdown-style list items (e.g.
- item , 1. item )
Parameters
$ignoreEscapes |
If false , preserve escaped whitespace.
|
$trimLines |
If true , remove whitespace from the end of each
line and between unwrapped lines.
|
$collapseBlankLines |
If true , collapse three or more
subsequent line breaks to two.
|
|
#
|
public
static
|
collapse(string $string): string
Replace whitespace character sequences in a string with a single space
Replace whitespace character sequences in a string with a single space
|
#
|
public
static
|
enclose(string $string, string $before, string|null $after = null): string
Enclose a string between delimiters
Enclose a string between delimiters
Parameters
$after |
If null , $before is used before and after
the string.
|
|
#
|
public
static
|
distance(string $string1, string $string2, bool $normalise = false): float
Get the Levenshtein distance between two strings relative to the length
of the longest string
Get the Levenshtein distance between two strings relative to the length
of the longest string
Parameters
$normalise |
If true , call {@see Str::normalise()} to
normalise $string1 and $string2 for comparison.
|
Returns
A value between 0 and 1 , where 0 means the strings
are identical, and 1 means they have no similarities.
|
#
|
public
static
|
similarity(string $string1, string $string2, bool $normalise = false): float
Get the similarity of two strings relative to the length of the longest
string
Get the similarity of two strings relative to the length of the longest
string
Parameters
$normalise |
If true , call {@see Str::normalise()} to
normalise $string1 and $string2 for comparison.
|
Returns
A value between 0 and 1 , where 0 means the strings
have no similarities, and 1 means they are identical.
|
#
|
public
static
|
ngramSimilarity(string $string1, string $string2, bool $normalise = false, int $size = 2): float
Get ngrams shared between two strings relative to the number of
ngrams in the longest string
Get ngrams shared between two strings relative to the number of
ngrams in the longest string
Parameters
$normalise |
If true , call {@see Str::normalise()} to
normalise $string1 and $string2 for comparison.
|
Returns
A value between 0 and 1 , where 0 means the strings
have no shared ngrams, and 1 means their ngrams are identical.
|
#
|
public
static
|
ngramIntersection(string $string1, string $string2, bool $normalise = false, int $size = 2): float
Get ngrams shared between two strings relative to the number of
ngrams in the shortest string
Get ngrams shared between two strings relative to the number of
ngrams in the shortest string
Parameters
$normalise |
If true , call {@see Str::normalise()} to
normalise $string1 and $string2 for comparison.
|
Returns
A value between 0 and 1 , where 0 means the strings
have no shared ngrams, and 1 means their ngrams are identical.
|
#
|
public
static
|
ngrams(string $string, int $size = 2): string[]
Get a string's n-grams
|
#
|
public
static
|
mergeLists(
string $string,
string $listSeparator = "\n",
string|null $headingPrefix = null,
?string $itemRegex = Str::DEFAULT_ITEM_REGEX,
bool $clean = false,
bool $loose = false,
bool $discardEmpty = false,
string $eol = "\n",
int $tabSize = 4,
): string
Group lists in a string by heading and remove duplicate items
Group lists in a string by heading and remove duplicate items
- Lines in
$text are processed in order, from first to last
- If a non-empty line matches
$itemRegex , it is treated as a list item,
otherwise it becomes the current heading
- The current heading is cleared when an empty line is encountered after
a list item (unless
$loose is true )
- Top-level lines (i.e. headings with no items, and items with no
heading) are returned before lists with headings
- If
$itemRegex has a named subpattern called indent that matches a
non-empty string, subsequent lines with indentation of the same width
are treated as a continuation of the item, along with any empty lines
between them
Parameters
$listSeparator |
Inserted between headings and lists.
|
$headingPrefix |
Inserted before headings, e.g. "-" .
Indentation of the same width is applied to subsequent list items.
|
$clean |
If true , remove the first match of $itemRegex from
the beginning of each item with no heading.
|
$loose |
If true , do not clear the current heading when an
empty line is encountered.
|
$discardEmpty |
If true , discard headings with no items.
|
|
#
|