class Strings

String tools library.

Traits

Static class.

Constants

TRIM_CHARACTERS

Methods

static bool
checkEncoding(string $s)

Checks if the string is valid for UTF-8 encoding.

static string
fixEncoding(string $s)

Removes invalid code unit sequences from UTF-8 string.

static string
chr(int $code)

Returns a specific character in UTF-8 from code point (0x0 to 0xD7FF or 0xE000 to 0x10FFFF).

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

Starts the $haystack string with the prefix $needle?

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

Ends the $haystack string with the suffix $needle?

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

Does $haystack contain $needle?

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

Returns a part of UTF-8 string.

static string
normalize(string $s)

Removes special controls characters and normalizes line endings, spaces and normal form to NFC in UTF-8 string.

static string
normalizeNewLines(string $s)

Standardize line endings to unix-like.

static string
toAscii(string $s)

Converts UTF-8 string to ASCII.

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

Converts UTF-8 string to web safe characters [a-z0-9-] text.

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

Truncates UTF-8 string to maximal length.

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

Indents UTF-8 string from the left.

static string
lower(string $s)

Converts UTF-8 string to lower case.

static string
firstLower(string $s)

Converts first character to lower case.

static string
upper(string $s)

Converts UTF-8 string to upper case.

static string
firstUpper(string $s)

Converts first character to upper case.

static string
capitalize(string $s)

Capitalizes UTF-8 string.

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

Case-insensitive compares UTF-8 strings.

static string
findPrefix(array $strings)

Finds the length of common prefix of strings.

static int
length(string $s)

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

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

Strips whitespace from UTF-8 string.

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

Pad a UTF-8 string to a certain length with another string.

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

Pad a UTF-8 string to a certain length with another string.

static string
reverse(string $s)

Reverse string.

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

Returns part of $haystack before $nth occurence of $needle (negative value means searching from the end).

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

Returns part of $haystack after $nth occurence of $needle (negative value means searching from the end).

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

Returns position of $nth occurence of $needle in $haystack (negative value means searching from the end).

static array
split(string $subject, string $pattern, int $flags = 0)

Splits string by a regular expression.

static array|null
match(string $subject, string $pattern, int $flags = 0, int $offset = 0)

Performs a regular expression match. Accepts flag PREG_OFFSET_CAPTURE (returned in bytes).

static array
matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0)

Performs a global regular expression match. Accepts flag PREG_OFFSET_CAPTURE (returned in bytes), PREG_SET_ORDER is default.

static string
replace(string $subject, $pattern, $replacement = null, int $limit = -1)

Perform a regular expression search and replace.

static 
pcre(string $func, array $args)

No description

Details

at line 29
static bool checkEncoding(string $s)

Checks if the string is valid for UTF-8 encoding.

Parameters

string $s

Return Value

bool

at line 38
static string fixEncoding(string $s)

Removes invalid code unit sequences from UTF-8 string.

Parameters

string $s

Return Value

string

at line 49
static string chr(int $code)

Returns a specific character in UTF-8 from code point (0x0 to 0xD7FF or 0xE000 to 0x10FFFF).

Parameters

int $code

Return Value

string

Exceptions

InvalidArgumentException if code point is not in valid range

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

Starts the $haystack string with the prefix $needle?

Parameters

string $haystack
string $needle

Return Value

bool

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

Ends the $haystack string with the suffix $needle?

Parameters

string $haystack
string $needle

Return Value

bool

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

Does $haystack contain $needle?

Parameters

string $haystack
string $needle

Return Value

bool

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

Returns a part of UTF-8 string.

Parameters

string $s
int $start
int $length

Return Value

string

at line 104
static string normalize(string $s)

Removes special controls characters and normalizes line endings, spaces and normal form to NFC in UTF-8 string.

Parameters

string $s

Return Value

string

at line 129
static string normalizeNewLines(string $s)

Standardize line endings to unix-like.

Parameters

string $s

Return Value

string

at line 138
static string toAscii(string $s)

Converts UTF-8 string to ASCII.

Parameters

string $s

Return Value

string

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

Converts UTF-8 string to web safe characters [a-z0-9-] text.

Parameters

string $s
string $charlist
bool $lower

Return Value

string

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

Truncates UTF-8 string to maximal length.

Parameters

string $s
int $maxLen
string $append

Return Value

string

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

Indents UTF-8 string from the left.

Parameters

string $s
int $level
string $chars

Return Value

string

at line 226
static string lower(string $s)

Converts UTF-8 string to lower case.

Parameters

string $s

Return Value

string

at line 235
static string firstLower(string $s)

Converts first character to lower case.

Parameters

string $s

Return Value

string

at line 244
static string upper(string $s)

Converts UTF-8 string to upper case.

Parameters

string $s

Return Value

string

at line 253
static string firstUpper(string $s)

Converts first character to upper case.

Parameters

string $s

Return Value

string

at line 262
static string capitalize(string $s)

Capitalizes UTF-8 string.

Parameters

string $s

Return Value

string

at line 271
static bool compare(string $left, string $right, int $len = null)

Case-insensitive compares UTF-8 strings.

Parameters

string $left
string $right
int $len

Return Value

bool

at line 293
static string findPrefix(array $strings)

Finds the length of common prefix of strings.

Parameters

array $strings

Return Value

string

at line 314
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 323
static string trim(string $s, string $charlist = self::TRIM_CHARACTERS)

Strips whitespace from UTF-8 string.

Parameters

string $s
string $charlist

Return Value

string

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

Pad a UTF-8 string to a certain length with another string.

Parameters

string $s
int $length
string $pad

Return Value

string

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

Pad a UTF-8 string to a certain length with another string.

Parameters

string $s
int $length
string $pad

Return Value

string

at line 355
static string reverse(string $s)

Reverse string.

Parameters

string $s

Return Value

string

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

Returns part of $haystack before $nth occurence of $needle (negative value means searching from the end).

Parameters

string $haystack
string $needle
int $nth

Return Value

string|null returns null if the needle was not found

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

Returns part of $haystack after $nth occurence of $needle (negative value means searching from the end).

Parameters

string $haystack
string $needle
int $nth

Return Value

string|null returns null if the needle was not found

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

Returns position of $nth occurence of $needle in $haystack (negative value means searching from the end).

Parameters

string $haystack
string $needle
int $nth

Return Value

int|null offset in characters or null if the needle was not found

at line 433
static array split(string $subject, string $pattern, int $flags = 0)

Splits string by a regular expression.

Parameters

string $subject
string $pattern
int $flags

Return Value

array

at line 442
static array|null match(string $subject, string $pattern, int $flags = 0, int $offset = 0)

Performs a regular expression match. Accepts flag PREG_OFFSET_CAPTURE (returned in bytes).

Parameters

string $subject
string $pattern
int $flags
int $offset

Return Value

array|null

at line 456
static array matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0)

Performs a global regular expression match. Accepts flag PREG_OFFSET_CAPTURE (returned in bytes), PREG_SET_ORDER is default.

Parameters

string $subject
string $pattern
int $flags
int $offset

Return Value

array

at line 475
static string replace(string $subject, $pattern, $replacement = null, int $limit = -1)

Perform a regular expression search and replace.

Parameters

string $subject
$pattern
$replacement
int $limit

Return Value

string

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

Parameters

string $func
array $args