The TextHelper contains methods to make text more usable and friendly in your views. It aids in enabling links, formatting urls, creating excerpts of text around chosen words or phrases, highlighting key words in blocks of text, and to gracefully truncating long stretches of text.
excerpt(string $haystack, string $needle, int $radius=100, string $ending="...")
Extracts an excerpt from $haystack surrounding the $needle with a number of characters on each side determined by $radius, and suffixed with $ending. This method is especially handy for search results. The query string or keywords can be shown within the resulting document.
echo $this->Text->excerpt($last_paragraph, 'method', 50);
mined by $radius, and suffixed with $ending. This method is especially handy for search results. The query...
highlight(string $haystack, string $needle, array $options = array() )
Highlights $needle in $haystack using the $options['format'] string specified or a default string.
echo $this->Text->highlight($last_sentence, 'using',array('format'=>'<span class="highlight">\1</span>');
Highlights $needle in $haystack <span class="highlight">using</span> the $options['format'] string specified or a default string.
toList(array $list, $and='and', $separator=', ')
Creates a comma-separated list where the last two items are joined with ‘and’.
red, orange, yellow, green, blue, indigo and violet
truncate(string $text, int $length=100, array $options)
Cuts a string to the $length and adds a suffix with 'ending' if the text is longer than $length. If 'exact' is passed as false, the truncation will occur after the next word ending. If 'html' is passed as true, html tags will be respected and will not be cut off.
$options is used to pass all extra parameters, and has the following possible keys by default, all of which are optional:
array( 'ending' => '...', 'exact' => true, 'html' => false )
echo $this->Text->truncate( 'The killer crept forward and tripped on the rug.', 22, array( 'ending' => '...', 'exact' => false ) );
The killer crept...
An alias for truncate.