Text handling methods.
array<string>
Default HTML tags which must not be counted for truncating text.
Transliterator|null
Default transliterator.
string
Default transliterator id string.
Removes the last word from the input text.
Get string length.
Return part of a string.
Unicode aware version of wordwrap as helper method.
Converts the decimal value of a multibyte character string to a 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().
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
Get the default transliterator.
Get default transliterator identifier 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.
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.
Check if the string contain multibyte characters
Converts filesize from human readable string to bytes
Set the default transliterator.
Set default transliterator identifier string.
Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.
Truncates text starting from the end.
Creates a comma separated list where the last two items are joined with 'and', forming natural language.
Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound.
Transliterate string.
Truncates text.
Truncate text with specified width.
Converts a multibyte character string to the decimal value of the character
Generate a random UUID version 4
Unicode and newline aware version of wordwrap.
Wraps text to a specific width, can optionally wrap at word breaks.
Wraps a complete block of text to a specific width, can optionally wrap at word breaks.
_removeLastWord(string $text): string
Removes the last word from the input text.
string
$text The input text
string
_strlen(string $text, array<string, mixed> $options): int
Get string length.
html
If true, HTML entities will be handled as decoded characters.trimWidth
If true, the width will return.string
$text The string being checked for length
array<string, mixed>
$options An array of options.
int
_substr(string $text, int $start, int|null $length, array<string, mixed> $options): string
Return part of a string.
html
If true, HTML entities will be handled as decoded characters.trimWidth
If true, will be truncated with specified width.string
$text The input string.
int
$start The position to begin extracting.
int|null
$length The desired length.
array<string, mixed>
$options An array of options.
string
_wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string
Unicode aware version of wordwrap as helper method.
string
$text The text to format.
int
$width optional The width to wrap to. Defaults to 72.
string
$break optional The line is broken using the optional break parameter. Defaults to '\n'.
bool
$cut optional If the cut is set to true, the string is always wrapped at the specified width.
string
ascii(array $array): string
Converts the decimal value of a multibyte character string to a string
array
$array Array
string
cleanInsert(string $str, array<string, mixed> $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().
string
$str String to clean.
array<string, mixed>
$options Options list.
string
excerpt(string $text, string $phrase, int $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.
string
$text String to search the phrase in
string
$phrase Phrase that will be searched for
int
$radius optional The amount of characters that will be returned on each side of the founded phrase
string
$ellipsis optional Ending that will be appended
string
getTransliterator(): Transliterator|null
Get the default transliterator.
Transliterator|null
getTransliteratorId(): string
Get default transliterator identifier string.
string
highlight(string $text, array<string>|string $phrase, array<string, mixed> $options = []): 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.
format
The piece of HTML with that the phrase will be highlightedhtml
If true, will ignore any HTML tags, ensuring that only the correct text is highlightedregex
A custom regex rule that is used to match words, default is '|$tag|iu'limit
A limit, optional, defaults to -1 (none)string
$text Text to search the phrase in.
array<string>|string
$phrase The phrase or phrases that will be searched.
array<string, mixed>
$options optional An array of HTML attributes and options.
string
insert(string $str, array $data, array<string, mixed> $options = []): 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:
:
)\
)/(?<!\\)\:%s/
(Overwrites before, after, breaks escape / clean)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<string, mixed>
$options optional An array of options, see description above
string
isMultibyte(string $string): bool
Check if the string contain multibyte characters
string
$string value to test
bool
parseFileSize(string $size, mixed $default = false): mixed
Converts filesize from human readable string to bytes
string
$size Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
mixed
$default optional Value to be returned when invalid size was used, for example 'Unknown type'
mixed
InvalidArgumentException
setTransliterator(Transliterator $transliterator): void
Set the default transliterator.
Transliterator
$transliterator A Transliterator
instance.
void
setTransliteratorId(string $transliteratorId): void
Set default transliterator identifier string.
string
$transliteratorId Transliterator identifier.
void
slug(string $string, array<string, mixed>|string $options = []): string
Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.
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.string
$string the string you want to slug
array<string, mixed>|string
$options optional If string it will be use as replacement character or an array of options.
string
tail(string $text, int $length = 100, array<string, mixed> $options = []): 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.
ellipsis
Will be used as beginning and prepended to the trimmed stringexact
If false, $text will not be cut mid-wordstring
$text String to truncate.
int
$length optional Length of returned string, including ellipsis.
array<string, mixed>
$options optional An array of options.
string
toList(array<string> $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.
array<string>
$list The list to be joined.
string|null
$and optional The word used to join the last and second last items together with. Defaults to 'and'.
string
$separator optional The separator used to join all the other items together. Defaults to ', '.
string
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.
string
$data The data to tokenize.
string
$separator optional The token to split the data on.
string
$leftBound optional The left boundary to ignore separators in.
string
$rightBound optional The right boundary to ignore separators in.
array<string>
transliterate(string $string, Transliterator|string|null $transliterator = null): string
Transliterate string.
string
$string String to transliterate.
Transliterator|string|null
$transliterator optional Either a Transliterator instance, or a transliterator identifier string. If null
, the default transliterator (identifier) set via setTransliteratorId()
or setTransliterator()
will be used.
string
truncate(string $text, int $length = 100, array<string, mixed> $options = []): 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.
ellipsis
Will be used as ending and appended to the trimmed stringexact
If false, $text will not be cut mid-wordhtml
If true, HTML tags would be handled correctlytrimWidth
If true, $text will be truncated with the widthstring
$text String to truncate.
int
$length optional Length of returned string, including ellipsis.
array<string, mixed>
$options optional An array of HTML attributes and options.
string
truncateByWidth(string $text, int $length = 100, array<string, mixed> $options = []): string
Truncate text with specified width.
string
$text String to truncate.
int
$length optional Length of returned string, including ellipsis.
array<string, mixed>
$options optional An array of HTML attributes and options.
string
utf8(string $string): array<int>
Converts a multibyte character string to the decimal value of the character
string
$string String to convert.
array<int>
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 Security::randomBytes()
or Security::randomString()
instead.
It should also not be used to create identifiers that have security implications, such as 'unguessable' URL identifiers. Instead, you should use {@link \Cake\Utility\Security::randomBytes()}` for that.
string
wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string
Unicode and newline aware version of wordwrap.
string
$text The text to format.
int
$width optional The width to wrap to. Defaults to 72.
string
$break optional The line is broken using the optional break parameter. Defaults to '\n'.
bool
$cut optional If the cut is set to true, the string is always wrapped at the specified width.
string
wrap(string $text, array<string, mixed>|int $options = []): string
Wraps text to a specific width, can optionally wrap at word breaks.
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.string
$text The text to format.
array<string, mixed>|int
$options optional Array of options to use, or an integer to wrap the text to.
string
wrapBlock(string $text, array<string, mixed>|int $options = []): string
Wraps a complete block of text to a specific width, can optionally wrap at word breaks.
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.string
$text The text to format.
array<string, mixed>|int
$options optional Array of options to use, or an integer to wrap the text to.
string
Default HTML tags which must not be counted for truncating text.
array<string>
Default transliterator.
Transliterator|null
Default transliterator id string.
string
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Utility.Text.html