Class String
String handling methods.
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/Utility/String.php
Method Summary
-
cleanInsert() public static
Cleans up a String::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 String::insert().
-
excerpt() public static
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
-
highlight() public static
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.
-
insert() public static
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:
String::insert(':name is :age years old.', array('name' => 'Bob', '65'));
Returns: Bob is 65 years old. -
stripLinks() public static
Strips given text of all links (<a href=....) -
toList() public static
Creates a comma separated list where the last two items are joined with 'and', forming natural English -
tokenize() public static
Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound
-
truncate() public static
Truncates text. -
uuid() public static
Generate a random UUID -
wrap() public static
Wraps text to a specific width, can optionally wrap at word breaks.
Method Detail
cleanInsert() public static ¶
cleanInsert( string $str , string $options )
Cleans up a String::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 String::insert().
Parameters
- string $str
- string $options
Returns
See
excerpt() public static ¶
excerpt( string $text , string $phrase , integer $radius = 100 , string $ending = '...' )
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 $ending optional '...'
- Ending that will be appended
Returns
Modified string
Link
highlight() public static ¶
highlight( string $text , string $phrase , array $options = array() )
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 highlightedhtml
If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
Parameters
- string $text
- Text to search the phrase in
- string $phrase
- The phrase that will be searched
- array $options optional array()
- An array of html attributes and options.
Returns
The highlighted text
Link
insert() public static ¶
insert( string $str , string $data , string $options = array() )
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: String::insert(':name is :age years old.', array('name' => 'Bob', '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 String::cleanInsert
Parameters
- string $str
- A string containing variable placeholders
- string $data
A key => val array where each key stands for a placeholder variable name to be replaced with val
- string $options optional array()
- An array of options, see description above
Returns
stripLinks() public static ¶
stripLinks( string $text )
Strips given text of all links (<a href=....)
Parameters
- string $text
- Text
Returns
The text without links
Link
toList() public static ¶
toList( array $list , string $and = 'and' , string $separator = ', ' )
Creates a comma separated list where the last two items are joined with 'and', forming natural English
Parameters
- array $list
- The list to be joined
- string $and optional 'and'
- 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
The glued together string.
Link
tokenize() 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 of tokens in $data.
truncate() public static ¶
truncate( string $text , integer $length = 100 , array $options = array() )
Truncates text.
Cuts a string to the length of $length and replaces the last characters with the ending if the text is longer than length.
Options:
ending
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 correctly
Parameters
- string $text
- String to truncate.
- integer $length optional 100
- Length of returned string, including ellipsis.
- array $options optional array()
- An array of html attributes and options.
Returns
Trimmed string.
Link
uuid() public static ¶
uuid( )
Generate a random UUID
Returns
4122 UUID
See
wrap() public static ¶
wrap( string $text , mixed $options = array() )
Wraps text to a specific width, can optionally wrap at word breaks.
Options
width
The width to wrap to. Defaults to 72wordWrap
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
- Text the text to format.
- mixed $options optional array()
- Array of options to use, or an integer to wrap the text to.
Returns
Formatted text.