Text handling methods.
string
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.
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.
Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.
Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound.
Converts a multibyte character string to the decimal value of the character
Wraps a complete block of text to a specific width, can optionally wrap at word breaks.
_removeLastWord( string $text )
Removes the last word from the input text.
$text
_strlen( string $text , array $options )
Get string length.
html
If true, HTML entities will be handled as decoded characters.trimWidth
If true, the width will return.$text
$options
_substr( string $text , integer $start , integer $length , array $options )
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.$text
$start
$length
$options
_wordWrap( string $text , integer $width 72 , string $break "\n" , boolean $cut false )
Unicode aware version of wordwrap as helper method.
$text
$width
optional 72 $break
optional "\n" $cut
optional false ascii( array $array )
Converts the decimal value of a multibyte character string to a string
$array
cleanInsert( string $str , array $options )
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().
$str
$options
excerpt( string $text , string $phrase , integer $radius 100 , string $ellipsis '...' )
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
$text
$phrase
$radius
optional 100 $ellipsis
optional '...' getTransliteratorId( )
Get default transliterator identifier string.
highlight( string $text , string|array $phrase , array $options [] )
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'$text
$phrase
$options
optional [] insert( string $str , array $data , array $options [] )
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)$str
$data
A key => val array where each key stands for a placeholder variable name to be replaced with val
$options
optional [] isMultibyte( string $string )
Check if the string contain multibyte characters
$string
parseFileSize( string $size , mixed $default false )
Converts filesize from human readable string to bytes
$size
$default
optional false $default
on failure if not falsesetTransliteratorId( string $transliteratorId )
Set default transliterator identifier string.
$transliteratorId
slug( string $string , array $options [] )
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 tranliterator id string. If default null
Text::$_defaultTransliteratorId to 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
$options
optional [] If string it will be use as replacement character or an array of options.
stripLinks( string $text )
Strips given text of all links (<a href=....).
Warning This method is not an robust solution in preventing XSS or malicious HTML.
$text
tail( string $text , integer $length 100 , array $options [] )
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-word$text
$length
optional 100 $options
optional [] toList( array $list , string|null $and null , string $separator ', ' )
Creates a comma separated list where the last two items are joined with 'and', forming natural language.
$list
$and
optional null $separator
optional ', ' tokenize( string $data , string $separator ',' , string $leftBound '(' , string $rightBound ')' )
Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound.
$data
$separator
optional ',' $leftBound
optional '(' $rightBound
optional ')' transliterate( string $string , string|null $transliteratorId null )
Transliterate string.
$string
$transliteratorId
optional null Transliterator identifier. If null Text::$_defaultTransliteratorId will be used.
truncate( string $text , integer $length 100 , array $options [] )
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 width$text
$length
optional 100 $options
optional [] truncateByWidth( string $text , integer $length 100 , array $options [] )
Truncate text with specified width.
$text
$length
optional 100 $options
optional [] utf8( string $string )
Converts a multibyte character string to the decimal value of the character
$string
uuid( )
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.
wordWrap( string $text , integer $width 72 , string $break "\n" , boolean $cut false )
Unicode and newline aware version of wordwrap.
$text
$width
optional 72 $break
optional "\n" $cut
optional false wrap( string $text , array|integer $options [] )
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.$text
$options
optional [] wrapBlock( string $text , array|integer $options [] )
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.$text
$options
optional [] protected static string
Default transliterator id string.
'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'
© 2005–2017 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/3.4/class-Cake.Utility.Text.html