Class TextHelper

Text helper library.

Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...

AppHelper
Extended by TextHelper
Package: Cake\View\Helper
Link: https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
See: String
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/View/Helper/TextHelper.php

Properties summary

  • $_engine protected
    stdClass
    CakeText utility instance
  • $_placeholders protected
    array

    An array of md5sums and their contents. Used when inserting links into text.

  • $helpers public
    array
    helpers

Magic properties summary

Method Summary

  • __call() public
    Call methods from CakeText utility class
  • __construct() public
    Constructor
  • Saves the placeholder for a string, for later use. This gets around double escaping content in URL's.

  • _linkEmails() protected
    Links email addresses
  • _linkUrls() protected
    Replace placeholders with links.
  • autoLink() public
    Convert all links and email addresses to HTML links.
  • Adds email links (<a href="mailto:....) to a given text.
  • Adds links (<a href=....) to a given text, by finding text that begins with strings like http:// and ftp://.

  • Formats paragraphs around given text for all line breaks
    added for single line return

    <

    p> added for double line return

  • excerpt() public

    Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.

  • 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=....)
  • tail() public
    Truncates text starting from the end.
  • toList() public
    Creates a comma separated list where the last two items are joined with 'and', forming natural language.
  • truncate() public
    Truncates text.

Method Detail

__call()source public

__call( string $method , array $params )

Call methods from CakeText utility class

Parameters

string $method
Method to call.
array $params
Parameters to pass to method.

Returns

mixed
Whatever is returned by called method, or false on failure

__construct()source public

__construct( View $View , array $settings array() )

Constructor

Settings:

  • engine Class name to use to replace CakeText functionality. The class needs to be placed in the Utility directory.

Parameters

View $View
the view object the helper is attached to.
array $settings optional array()
Settings array Settings array

Throws

CakeException
when the engine class could not be found.

_insertPlaceHolder()source protected

_insertPlaceHolder( array $matches )

Saves the placeholder for a string, for later use. This gets around double escaping content in URL's.

Parameters

array $matches
An array of regexp matches.

Returns

string
Replaced values.

_linkEmails()source protected

_linkEmails( string $text , array $options )

Links email addresses

Parameters

string $text
The text to operate on
array $options
An array of options to use for the HTML.

Returns

string

See

TextHelper::autoLinkEmails()

_linkUrls()source protected

_linkUrls( string $text , array $htmlOptions )

Replace placeholders with links.

Parameters

string $text
The text to operate on.
array $htmlOptions
The options for the generated links.

Returns

string
The text with links inserted.
autoLink( string $text , array $options array() )

Convert all links and email addresses to HTML links.

Options

  • escape Control HTML escaping of input. Defaults to true.

Parameters

string $text
Text
array $options optional array()
Array of HTML options, and options listed above.

Returns

string
The text with links

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink

autoLinkEmails()source public

autoLinkEmails( string $text , array $options array() )

Adds email links (<a href="mailto:....) to a given text.

Options

  • escape Control HTML escaping of input. Defaults to true.

Parameters

string $text
Text
array $options optional array()
Array of HTML options, and options listed above.

Returns

string
The text with links

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails

autoLinkUrls()source public

autoLinkUrls( string $text , array $options array() )

Adds links (<a href=....) to a given text, by finding text that begins with strings like http:// and ftp://.

Options

  • escape Control HTML escaping of input. Defaults to true.

Parameters

string $text
Text
array $options optional array()
Array of HTML options, and options listed above.

Returns

string
The text with links

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls

autoParagraph()source public

autoParagraph( string $text )

Formats paragraphs around given text for all line breaks
added for single line return

<

p> added for double line return

Parameters

string $text
Text

Returns

string

The text with proper

<

p> and
tags


Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph

excerpt()source 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

string
Modified string

See

CakeText::excerpt()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt

highlight()source public

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.

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

string
The highlighted text

See

CakeText::highlight()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
stripLinks( string $text )

Strips given text of all links (<a href=....)

Parameters

string $text
Text

Returns

string
The text without links

See

CakeText::stripLinks()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks

tail()source public

tail( string $text , integer $length 100 , array $options array() )

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 array()
An array of html attributes and options.

Returns

string
Trimmed string.

See

CakeText::tail()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::tail

toList()source public

toList( array $list , string $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 $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

string
The glued together string.

See

CakeText::toList()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList

truncate()source public

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 ellipsis if the text is longer than length.

Options:

  • ellipsis Will be used as Ending and appended to the trimmed string (ending is deprecated)
  • exact If false, $text will not be cut mid-word
  • html 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

string
Trimmed string.

See

CakeText::truncate()

Link

https://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate

Properties detail

$_enginesource

protected stdClass

CakeText utility instance

$_placeholderssource

protected array

An array of md5sums and their contents. Used when inserting links into text.

array()

$helperssource

public array

helpers

array('Html')

Magic properties detail

$Htmlsource

public HtmlHelper

© 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/2.10/class-TextHelper.html