Properties

$_defaultTransliterator

$_defaultTransliterator : \Transliterator

Default transliterator.

Type

\Transliterator — Transliterator instance.

$_defaultTransliteratorId

$_defaultTransliteratorId : string

Default transliterator id string.

Type

string — Transliterator identifier string.

$_defaultHtmlNoCount

$_defaultHtmlNoCount : array

Default html tags who must not be count for truncate text.

Type

array

Methods

uuid()

uuid() : string

Generate a random UUID version 4

Warning: This method should not be used as a random seed for any cryptographic operations. Instead you should use the openssl or mcrypt extensions.

It should also not be used to create identifiers that have security implications, such as 'unguessable' URL identifiers. Instead you should use Security::randomBytes() for that.

Returns

string —

RFC 4122 UUID

tokenize()

tokenize(string  $data, string  $separator = ',', string  $leftBound = '(', string  $rightBound = ')') : array|string

Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound.

Parameters

string $data

The data to tokenize.

string $separator

The token to split the data on.

string $leftBound

The left boundary to ignore separators in.

string $rightBound

The right boundary to ignore separators in.

Returns

array|string —

Array of tokens in $data or original input if empty.

insert()

insert(string  $str, array  $data, array  $options = array()) : string

Replaces variable placeholders inside a $str with any given $data. Each key in the $data array corresponds to a variable placeholder name in $str.

Example:

Text::insert(':name is :age years old.', ['name' => 'Bob', 'age' => '65']);

Returns: Bob is 65 years old.

Available $options are:

  • before: The character or string in front of the name of the variable placeholder (Defaults to :)
  • after: The character or string after the name of the variable placeholder (Defaults to null)
  • escape: The character or string used to escape the before character / string (Defaults to \)
  • format: A regex to use for matching variable placeholders. Default is: /(?<!\\)\:%s/ (Overwrites before, after, breaks escape / clean)
  • clean: A boolean or array with instructions for Text::cleanInsert

Parameters

string $str

A string containing variable placeholders

array $data

A key => val array where each key stands for a placeholder variable name to be replaced with val

array $options

An array of options, see description above

Returns

string

cleanInsert()

cleanInsert(string  $str, array  $options) : string

Cleans up a Text::insert() formatted string with given $options depending on the 'clean' key in $options. The default method used is text but html is also available. The goal of this function is to replace all whitespace and unneeded markup around placeholders that did not get replaced by Text::insert().

Parameters

string $str

String to clean.

array $options

Options list.

Returns

string

wrap()

wrap(string  $text, array|integer  $options = array()) : string

Wraps text to a specific width, can optionally wrap at word breaks.

Options

  • width The width to wrap to. Defaults to 72.
  • wordWrap Only wrap on words breaks (spaces) Defaults to true.
  • indent String to indent with. Defaults to null.
  • indentAt 0 based index to start indenting at. Defaults to 0.

Parameters

string $text

The text to format.

array|integer $options

Array of options to use, or an integer to wrap the text to.

Returns

string —

Formatted text.

wrapBlock()

wrapBlock(string  $text, array|integer  $options = array()) : string

Wraps a complete block of text to a specific width, can optionally wrap at word breaks.

Options

  • width The width to wrap to. Defaults to 72.
  • wordWrap Only wrap on words breaks (spaces) Defaults to true.
  • indent String to indent with. Defaults to null.
  • indentAt 0 based index to start indenting at. Defaults to 0.

Parameters

string $text

The text to format.

array|integer $options

Array of options to use, or an integer to wrap the text to.

Returns

string —

Formatted text.

wordWrap()

wordWrap(string  $text, integer  $width = 72, string  $break = "\n", boolean  $cut = false) : string

Unicode and newline aware version of wordwrap.

Parameters

string $text

The text to format.

integer $width

The width to wrap to. Defaults to 72.

string $break

The line is broken using the optional break parameter. Defaults to '\n'.

boolean $cut

If the cut is set to true, the string is always wrapped at the specified width.

Returns

string —

Formatted text.

highlight()

highlight(string  $text, string|array  $phrase, array  $options = array()) : string

Highlights a given phrase in a text. You can specify any expression in highlighter that may include the \1 expression to include the $phrase found.

Options:

  • format The piece of HTML with that the phrase will be highlighted
  • html If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
  • regex A custom regex rule that is used to match words, default is '|$tag|iu'
  • limit A limit, optional, defaults to -1 (none)

Parameters

string $text

Text to search the phrase in.

string|array $phrase

The phrase or phrases that will be searched.

array $options

An array of HTML attributes and options.

Returns

string —

The highlighted text

stripLinks()

stripLinks(string  $text) : string

Strips given text of all links (<a href=.

...).

Warning This method is not an robust solution in preventing XSS or malicious HTML.

Parameters

string $text

Text

Returns

string —

The text without links

tail()

tail(string  $text, integer  $length = 100, array  $options = array()) : string

Truncates text starting from the end.

Cuts a string to the length of $length and replaces the first characters with the ellipsis if the text is longer than length.

Options:

  • ellipsis Will be used as Beginning and prepended to the trimmed string
  • exact If false, $text will not be cut mid-word

Parameters

string $text

String to truncate.

integer $length

Length of returned string, including ellipsis.

array $options

An array of options.

Returns

string —

Trimmed string.

truncate()

truncate(string  $text, integer  $length = 100, array  $options = array()) : string

Truncates text.

Cuts a string to the length of $length and replaces the last characters with the ellipsis if the text is longer than length.

Options:

  • ellipsis Will be used as ending and appended to the trimmed string
  • exact If false, $text will not be cut mid-word
  • html If true, HTML tags would be handled correctly
  • trimWidth If true, $text will be truncated with the width

Parameters

string $text

String to truncate.

integer $length

Length of returned string, including ellipsis.

array $options

An array of HTML attributes and options.

Returns

string —

Trimmed string.

truncateByWidth()

truncateByWidth(string  $text, integer  $length = 100, array  $options = array()) : string

Truncate text with specified width.

Parameters

string $text

String to truncate.

integer $length

Length of returned string, including ellipsis.

array $options

An array of HTML attributes and options.

Returns

string —

Trimmed string.

excerpt()

excerpt(string  $text, string  $phrase, integer  $radius = 100, string  $ellipsis = '...') : string

Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.

Parameters

string $text

String to search the phrase in

string $phrase

Phrase that will be searched for

integer $radius

The amount of characters that will be returned on each side of the founded phrase

string $ellipsis

Ending that will be appended

Returns

string —

Modified string

toList()

toList(array  $list, string|null  $and = null, string  $separator = ', ') : string

Creates a comma separated list where the last two items are joined with 'and', forming natural language.

Parameters

array $list

The list to be joined.

string|null $and

The word used to join the last and second last items together with. Defaults to 'and'.

string $separator

The separator used to join all the other items together. Defaults to ', '.

Returns

string —

The glued together string.

isMultibyte()

isMultibyte(string  $string) : boolean

Check if the string contain multibyte characters

Parameters

string $string

value to test

Returns

boolean

utf8()

utf8(string  $string) : array

Converts a multibyte character string to the decimal value of the character

Parameters

string $string

String to convert.

Returns

array

ascii()

ascii(array  $array) : string

Converts the decimal value of a multibyte character string to a string

Parameters

array $array

Array

Returns

string

parseFileSize()

parseFileSize(string  $size, mixed  $default = false) : mixed

Converts filesize from human readable string to bytes

Parameters

string $size

Size in human readable string like '5MB', '5M', '500B', '50kb' etc.

mixed $default

Value to be returned when invalid size was used, for example 'Unknown type'

Throws

\InvalidArgumentException

On invalid Unit type.

Returns

mixed —

Number of bytes as integer on success, $default on failure if not false

getTransliterator()

getTransliterator() : \Transliterator|null

Get the default transliterator.

Returns

\Transliterator|null —

Either a Transliterator instance, or null in case no transliterator has been set yet.

setTransliterator()

setTransliterator(\Transliterator  $transliterator) : void

Set the default transliterator.

Parameters

\Transliterator $transliterator

A Transliterator instance.

getTransliteratorId()

getTransliteratorId() : string

Get default transliterator identifier string.

Returns

string —

Transliterator identifier.

setTransliteratorId()

setTransliteratorId(string  $transliteratorId) : void

Set default transliterator identifier string.

Parameters

string $transliteratorId

Transliterator identifier.

transliterate()

transliterate(string  $string, \Transliterator|string|null  $transliterator = null) : string

Transliterate string.

Parameters

string $string

String to transliterate.

\Transliterator|string|null $transliterator

Either a Transliterator instance, or a transliterator identifier string. If null, the default transliterator (identifier) set via setTransliteratorId() or setTransliterator() will be used.

Returns

string

slug()

slug(string  $string, array  $options = array()) : string

Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.

Options:

  • replacement: Replacement string. Default '-'.
  • transliteratorId: A valid transliterator id string. If null (default) the transliterator (identifier) set via setTransliteratorId() or setTransliterator() will be used. If false no transliteration will be done, only non words will be removed.
  • preserve: Specific non-word character to preserve. Default null. For e.g. this option can be set to '.' to generate clean file names.

Parameters

string $string

the string you want to slug

array $options

If string it will be use as replacement character or an array of options.

Returns

string

_wordWrap()

_wordWrap(string  $text, integer  $width = 72, string  $break = "\n", boolean  $cut = false) : string

Unicode aware version of wordwrap as helper method.

Parameters

string $text

The text to format.

integer $width

The width to wrap to. Defaults to 72.

string $break

The line is broken using the optional break parameter. Defaults to '\n'.

boolean $cut

If the cut is set to true, the string is always wrapped at the specified width.

Returns

string —

Formatted text.

_strlen()

_strlen(string  $text, array  $options) : integer

Get string length.

Options:

  • html If true, HTML entities will be handled as decoded characters.
  • trimWidth If true, the width will return.

Parameters

string $text

The string being checked for length

array $options

An array of options.

Returns

integer

_substr()

_substr(string  $text, integer  $start, integer  $length, array  $options) : string

Return part of a string.

Options:

  • html If true, HTML entities will be handled as decoded characters.
  • trimWidth If true, will be truncated with specified width.

Parameters

string $text

The input string.

integer $start

The position to begin extracting.

integer $length

The desired length.

array $options

An array of options.

Returns

string

_removeLastWord()

_removeLastWord(string  $text) : string

Removes the last word from the input text.

Parameters

string $text

The input text

Returns

string