/php-text

PHP Text: string manipulation for humans

Primary LanguagePHPMIT LicenseMIT

PHP Text: string manipulation for humans

Latest Version PHP Version MIT License

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.

Contents

String manipulation functions

Text::convert(string $string, string $encoding, [$from])

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.

Return string

Text::format(string $format, array $values)

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.

Return string

Text::indexOf(string $substring, string $string, [int $offset], [bool $case_sensitive])

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.

Return int

A return value of -1 means that the substing was not found.

Text::isRegEx(string $pattern)

Check whether a string is a valid regular expression or not.

Parameter Type Description
$pattern string The string pattern.

Return bool

Text::join(array $array, [string $string])

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.

Return string

Text::length(string $string, [string $encoding])

Get a string length.

Parameter Type Description
$string string The string being measured.
$encoding string Optional.
The character encoding.

Return int

Text::lowercase(string $string)

Convert a string to lowercase.

Parameter Type Description
$string string The string to be converted to lowercase.

Return string

Text::match(string $string, string $pattern, [array &$groups])

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.

Return string|null

The full mathching string.

Text::matchAll(string $string, string $pattern, [array &$groups])

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.

Return array

Returns an array of all the full matches indexed by offset.

Text::pad(string $string, int $length, [string $chars], [int $side])

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.

Return string

Text::split(string $string, string $pattern, [int $limit])

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).

Return array

Text::style(string $string, [int $style])

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 string

Text::substring(string $string, [int $start], [int $length])

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.

Return string

Text::trim(string $string, [string $chars], [int $side])

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.

Return string

Text::uppercase(string $string)

Convert a string to uppercase.

Parameter Type Description
$string string The string to be converted to uppercase.

Return string

Text::wrap(string $string, int $length, [string $break], [int $cut])

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.

Return string

Locale settings functions

WARNING: These functions apply to the staticText class only and DO NOT affect PHP locale settings.

Text::getLocale([string $key])

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.

Return mixed

Text::getSystemLocale()

Get settings as per system locale.

Return array

Text::setLocale(array $locale)

Set locale Settings.

Parameter Type Description
$locale array Array of locale settings.
New settings are merged with current settings.