Text (class)
Class Text
Text handling methods.
Properties summary
- Default transliterator id string.
string
Method Summary
- 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 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 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.
- Strips given text of all links (<a href=....).
- 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.
Method Detail
_removeLastWord()source protected static
_removeLastWord( string $text )
Removes the last word from the input text.
Parameters
- string
$text
- The input text
Returns
string_strlen()source protected static
_strlen( string $text , array $options )
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
string_substr()source protected static
_substr( string $text , integer $start , integer $length , array $options )
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_wordWrap()source protected static
_wordWrap( string $text , integer $width 72 , string $break "\n" , boolean $cut false )
Unicode aware version of wordwrap as helper method.
Parameters
- string
$text
- The text to format.
- integer
$width
optional 72 - The width to wrap to. Defaults to 72.
- string
$break
optional "\n" - The line is broken using the optional break parameter. Defaults to '\n'.
- boolean
$cut
optional false - If the cut is set to true, the string is always wrapped at the specified width.
Returns
stringFormatted text.
ascii()source public static
ascii( array $array )
Converts the decimal value of a multibyte character string to a string
Parameters
- array
$array
- Array
Returns
stringcleanInsert()source public static
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().
Parameters
- string
$str
- String to clean.
- array
$options
- Options list.
Returns
stringSee
\Cake\Utility\Text::insert()excerpt()source public static
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.
Parameters
- string
$text
- String to search the phrase in
- string
$phrase
- Phrase that will be searched for
- integer
$radius
optional 100 - The amount of characters that will be returned on each side of the founded phrase
- string
$ellipsis
optional '...' - Ending that will be appended
Returns
stringModified string
Link
http://book.cakephp.org/3.0/en/core-libraries/string.html#extracting-an-excerptgetTransliteratorId()source public static
getTransliteratorId( )
Get default transliterator identifier string.
Returns
stringTransliterator identifier.
highlight()source public static
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.
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'
Parameters
- string
$text
- Text to search the phrase in.
- string|array
$phrase
- The phrase or phrases that will be searched.
- array
$options
optional [] - An array of HTML attributes and options.
Returns
stringThe highlighted text
Link
http://book.cakephp.org/3.0/en/core-libraries/string.html#highlighting-substringsinsert()source public static
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:
- 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
optional [] - An array of options, see description above
Returns
stringisMultibyte()source public static
isMultibyte( string $string )
Check if the string contain multibyte characters
Parameters
- string
$string
- value to test
Returns
booleanparseFileSize()source public static
parseFileSize( string $size , mixed $default false )
Converts filesize from human readable string to bytes
Parameters
- string
$size
- Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
- mixed
$default
optional false - Value to be returned when invalid size was used, for example 'Unknown type'
Returns
mixedNumber of bytes as integer on success,
$default
on failure if not falseThrows
InvalidArgumentExceptionOn invalid Unit type.
Link
http://book.cakephp.org/3.0/en/core-libraries/helpers/text.htmlsetTransliteratorId()source public static
setTransliteratorId( string $transliteratorId )
Set default transliterator identifier string.
Parameters
- string
$transliteratorId
- Transliterator identifier.
slug()source public static
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.
Options:
-
replacement
: Replacement string. Default '-'. -
transliteratorId
: A valid tranliterator id string. If defaultnull
Text::$_defaultTransliteratorId to be used. Iffalse
no transliteration will be done, only non words will be removed. -
preserve
: Specific non-word character to preserve. Defaultnull
. 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
optional [] If string it will be use as replacement character or an array of options.
Returns
stringstripLinks()source public static
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.
Deprecated
3.2.12 This method will be removed in 4.0.0Parameters
- string
$text
- Text
Returns
stringThe text without links
tail()source public static
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.
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
optional 100 - Length of returned string, including ellipsis.
- array
$options
optional [] - An array of options.
Returns
stringTrimmed string.
toList()source public static
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.
Parameters
- array
$list
- The list to be joined.
- string|null
$and
optional null - 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 ', '.
Returns
stringThe glued together string.
Link
http://book.cakephp.org/3.0/en/core-libraries/string.html#converting-an-array-to-sentence-formtokenize()source public static
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.
Parameters
- 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.
Returns
array|stringArray of tokens in $data or original input if empty.
transliterate()source public static
transliterate( string $string , string|null $transliteratorId null )
Transliterate string.
Parameters
- string
$string
- String to transliterate.
- string|null
$transliteratorId
optional null Transliterator identifier. If null Text::$_defaultTransliteratorId will be used.
Returns
stringSee
http://php.net/manual/en/transliterator.transliterate.phptruncate()source public static
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.
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
optional 100 - Length of returned string, including ellipsis.
- array
$options
optional [] - An array of HTML attributes and options.
Returns
stringTrimmed string.
Link
http://book.cakephp.org/3.0/en/core-libraries/string.html#truncating-texttruncateByWidth()source public static
truncateByWidth( string $text , integer $length 100 , array $options [] )
Truncate text with specified width.
Parameters
- string
$text
- String to truncate.
- integer
$length
optional 100 - Length of returned string, including ellipsis.
- array
$options
optional [] - An array of HTML attributes and options.
Returns
stringTrimmed string.
See
\Cake\Utility\Text::truncate()utf8()source public static
utf8( string $string )
Converts a multibyte character string to the decimal value of the character
Parameters
- string
$string
- String to convert.
Returns
arrayuuid()source public static
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.
Returns
stringRFC 4122 UUID
See
http://www.ietf.org/rfc/rfc4122.txtCopyright
Matt Farina MIT License https://github.com/lootils/uuid/blob/master/LICENSEwordWrap()source public static
wordWrap( string $text , integer $width 72 , string $break "\n" , boolean $cut false )
Unicode and newline aware version of wordwrap.
Parameters
- string
$text
- The text to format.
- integer
$width
optional 72 - The width to wrap to. Defaults to 72.
- string
$break
optional "\n" - The line is broken using the optional break parameter. Defaults to '\n'.
- boolean
$cut
optional false - If the cut is set to true, the string is always wrapped at the specified width.
Returns
stringFormatted text.
wrap()source public static
wrap( string $text , array|integer $options [] )
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
optional [] - Array of options to use, or an integer to wrap the text to.
Returns
stringFormatted text.
wrapBlock()source public static
wrapBlock( string $text , array|integer $options [] )
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
optional [] - Array of options to use, or an integer to wrap the text to.
Returns
stringFormatted text.
Properties detail
$_defaultTransliteratorIdsource
protected static string
Default transliterator id string.
Param
string $_defaultTransliteratorId Transliterator identifier string.'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove'
© 2005–2016 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.
http://api.cakephp.org/3.2/class-Cake.Utility.Text.html