The methods in this library may not be as computationally efficient as the builtin PHP functions, but they are quite useful if you want to avoid some headaches.
Note: Some methods use the multi-byte safe built-in functions whenever possible.
See: php Multibyte String Functions.
- String manipulation functions
Text::convert(string $string, string $encoding, [$from])
Text::format(string $format, array $values)
Text::indexOf(string $substring, string $string, [int $offset], [bool $case_sensitive])
Text::isRegEx(string $pattern)
Text::join(array $array, [string $string])
Text::length(string $string, [string $encoding])
Text::lowercase(string $string)
Text::match(string $string, string $pattern, [array &$groups])
Text::matchAll(string $string, string $pattern, [array &$groups])
Text::pad(string $string, int $length, [string $chars], [int $side])
Text::split(string $string, string $pattern, [int $limit])
Text::style(string $string, [int $style])
Text::substring(string $string, [int $start], [int $length])
Text::trim(string $string, [string $chars], [int $side])
Text::uppercase(string $string)
Text::wrap(string $string, int $length, [string $break], [int $cut])
- Locale settings functions
Convert a string's character encoding.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$encoding |
string | The target encoding. |
$from |
string|string[] | Optional. The original encoding. It is either an array, or a comma separated enumerated list. If $from is not specified, then the internal encoding will be used. |
Format a string.
Parameter | Type | Description |
---|---|---|
$format |
string | A conversion specification follows the following prototypes: - %{key} or ${key} - %{key:modifier} or ${key:modifier} Keys in the format string must match the appropriate value key. Nested values can be referenced via dot notation. |
$values |
array | The replacemente values. The array can be either indexed or associative. |
Find the position of a substring in a string.
Parameter | Type | Description |
---|---|---|
$substring |
string | The substring to search for. |
$string |
string | The string to search in. |
$offset |
int | Optional. If positive, the search is performed left to right skipping the first offset bytes. If negative, the search is performed right to left skipping the last offset bytes. Default: 0 . |
$case_sensitive |
bool | Optional. If false , serarch will be case-insensitive. Default: true . |
A return value of -1
means that the substing was not found.
Check whether a string is a valid regular expression or not.
Parameter | Type | Description |
---|---|---|
$pattern |
string | The string pattern. |
Join array elements with a string.
Parameter | Type | Description |
---|---|---|
$array |
array | The array of strings to join. |
$string |
string | Optional. The string used to join the array items together. |
Get a string length.
Parameter | Type | Description |
---|---|---|
$string |
string | The string being measured. |
$encoding |
string | Optional. The character encoding. |
Convert a string to lowercase.
Parameter | Type | Description |
---|---|---|
$string |
string | The string to be converted to lowercase. |
Perform a string match.
Parameter | Type | Description |
---|---|---|
$string |
string | The input string. |
$pattern |
string | The characters or regex to search for. |
$groups |
array | Optional. Array of all captured groups. |
The full mathching string.
Perform a global string match.
Parameter | Type | Description |
---|---|---|
$string |
string | The input string. |
$pattern |
string | The characters or regex to search for. |
$groups |
array|null | Optional. Array of all captured groups indexed by global match offset. |
Returns an array of all the full matches indexed by offset.
Pad a string to a certain length with another string.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$length |
int | The string target length. |
$chars |
string | Optional. The padding characters. Default: ' ' |
$side |
int | Optional. The padding side ( Text::PAD_BOTH |Text::PAD_LEFT |Text::PAD_RIGHT ).Default: Text::PAD_LEFT . |
Split a string into chunks.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$pattern |
string | The characters or regex used as splitter. |
$limit |
int | Optional. The maximum number of cuts ( 0 - or negative numbers - means no limits). |
Apply a style to a string.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$style |
int | Optional. The style to be applied. Accepted values: - Text::STYLE_TEXT_PARAGRAPH - Text::STYLE_TEXT_TITLE - Text::STYLE_TEXT_LOWERCASE - Text::STYLE_TEXT_UPPERCASE - Text::STYLE_VAR_CAMEL_CASE - Text::STYLE_VAR_KEBAB_CASE - Text::STYLE_VAR_PASCAL_CASE - Text::STYLE_VAR_SNAKE_CASE - Text::STYLE_VAR_LOWER_CASE - Text::STYLE_VAR_UPPER_CASE Default: Text::STYLE_TEXT_PARAGRAPH . |
Return part of a string.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$start |
int | Optional. Start position. |
$length |
int | Optional. Length of the extracted string. |
Strip whitespace (or other characters) from the beginning and/or end of a string.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$chars |
string | Optional. Characters to be trimmed. Default: " \t\n\r\0\x0B" . |
$side |
int | Optional. The trim side ( Text::TRIM_BOTH |Text::TRIM_LEFT |Text::TRIM_RIGHT ).Default: Text::TRIM_BOTH . |
Convert a string to uppercase.
Parameter | Type | Description |
---|---|---|
$string |
string | The string to be converted to uppercase. |
Wrap a string to a given number of characters.
Parameter | Type | Description |
---|---|---|
$string |
string | The original string. |
$length |
int | The maximum length of a line. |
$break |
string | Optional. Line break character. Default: "\n" . |
$cut |
int | Optional. How to cut the line if a word exceeds maximum length. Accepted values: - Text::WRAP_AFTER : Wrap at first space after the specified length.- Text::WRAP_BEFORE : Wrap at last space before the specified length.- Text::WRAP_BREAK : Wrap exactly at the specified length.Default: Text::WRAP_BEFORE . |
WARNING: These functions apply to the staticText class only and DO NOT affect PHP locale settings.
Get locale settings.
Initially equals to system locale settings.
Parameter | Type | Description |
---|---|---|
$key |
string | Optional. If $key is provided then only the corresponding value is returned. |
Get settings as per system locale.
Set locale Settings.
Parameter | Type | Description |
---|---|---|
$locale |
array | Array of locale settings. New settings are merged with current settings. |