Class TextHelper
Text helper library.
Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
- AppHelper
- TextHelper
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: The MIT License
Location: view/helpers/text.php
Method Summary
-
autoLink() public
Convert all links and email adresses to HTML links. -
autoLinkEmails() public
Adds email links (<a href="mailto:....) to a given text. -
autoLinkUrls() public
Adds links (<a href=....) to a given text, by finding text that begins with strings like http:// and ftp://.
-
excerpt() public
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius. -
flay() public
Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax. -
highlight() public
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.
-
stripLinks() public
Strips given text of all links (<a href=....) -
toList() public
Creates a comma separated list where the last two items are joined with 'and', forming natural English -
trim() public
Alias for truncate(). -
truncate() public
Truncates text.
Method Detail
autoLink() public ¶
autoLink( string $text , array $htmlOptions = array() )
Convert all links and email adresses to HTML links.
Parameters
- string $text
- Text
- array $htmlOptions optional array()
- Array of HTML options.
Returns
The text with links
autoLinkEmails() public ¶
autoLinkEmails( string $text , array $htmlOptions = array() )
Adds email links (<a href="mailto:....) to a given text.
Parameters
- string $text
- Text
- array $htmlOptions optional array()
- Array of HTML options.
Returns
The text with links
autoLinkUrls() public ¶
autoLinkUrls( string $text , array $htmlOptions = array() )
Adds links (<a href=....) to a given text, by finding text that begins with strings like http:// and ftp://.
Parameters
- string $text
- Text to add links to
- array $htmlOptions optional array()
- Array of HTML options.
Returns
The text with links
excerpt() public ¶
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
flay() public ¶
flay( string $text , boolean $allowHtml = false )
Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
Parameters
- string $text
- String to "flay"
- boolean $allowHtml optional false
- Set to true if if html is allowed
Returns
"Flayed" text
Codecoverageignorestart
highlight() public ¶
highlight( string $text , string $phrase , string $highlighter = '<span class="highlight">\1</span>' , boolean $considerHtml = false )
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.
Parameters
- string $text
- Text to search the phrase in
- string $phrase
- The phrase that will be searched
- string $highlighter optional '<span class="highlight">\1</span>'
- The piece of html with that the phrase will be highlighted
- boolean $considerHtml optional false
- If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
Returns
The highlighted text
stripLinks() public ¶
stripLinks( string $text )
Strips given text of all links (<a href=....)
Parameters
- string $text
- Text
Returns
The text without links
toList() public ¶
toList( array $list , $and = 'and' )
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
- $and optional 'and'
Returns
truncate() public ¶
truncate( string $text , integer $length = 100 , mixed $ending = '...' , boolean $exact = true , boolean $considerHtml = false )
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.
Parameters
- string $text
- String to truncate.
- integer $length optional 100
- Length of returned string, including ellipsis.
- mixed $ending optional '...'
- If string, will be used as Ending and appended to the trimmed string. Can also be an associative array that can contain the last three params of this method.
- boolean $exact optional true
- If false, $text will not be cut mid-word
- boolean $considerHtml optional false
- If true, HTML tags would be handled correctly
Returns
Trimmed string.