Fr3nch13/CakePHP Utilities

TextHelper extends Helper
in package

Text helper library.

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

Tags
link
https://book.cakephp.org/4/en/views/helpers/text.html
see
Text

Table of Contents

Properties

$Html  : HtmlHelper
$_config  : array<string, mixed>
Runtime config
$_configInitialized  : bool
Whether the config property has already been configured with defaults
$_defaultConfig  : array<string, mixed>
Default config for this class
$_engine  : Text
Cake Utility Text instance
$_helperMap  : array<string, array<string|int, mixed>>
A helper lookup table used to lazy load helper objects.
$_placeholders  : array<string, array<string|int, mixed>>
An array of hashes and their contents.
$_View  : View
The View instance this helper is attached to
$helpers  : array<string|int, mixed>
helpers

Methods

__call()  : mixed
Call methods from String utility class
__construct()  : mixed
Constructor
__debugInfo()  : array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
__get()  : Helper|null|void
Lazy loads helpers.
addClass()  : array<string, mixed>
Adds the given class to the element options
autoLink()  : string
Convert all links and email addresses to HTML links.
autoLinkEmails()  : string
Adds email links (<a href="mailto:....") to a given text.
autoLinkUrls()  : string
Adds links (<a href=....) to a given text, by finding text that begins with strings like http:// and ftp://.
autoParagraph()  : string
Formats paragraphs around given text for all line breaks <br /> added for single line return <p> added for double line return
configShallow()  : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
excerpt()  : string
Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
getConfig()  : mixed
Returns the config.
getConfigOrFail()  : mixed
Returns the config for this specific key.
getView()  : View
Get the view instance this helper is bound to.
highlight()  : 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.
implementedEvents()  : array<string, mixed>
Event listeners.
initialize()  : void
Constructor hook method.
setConfig()  : $this
Sets the config.
slug()  : string
Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.
tail()  : string
Truncates text starting from the end.
toList()  : string
Creates a comma separated list where the last two items are joined with 'and', forming natural language.
truncate()  : string
Truncates text.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.
_confirm()  : string
Returns a string to be used as onclick handler for confirm dialogs.
_insertPlaceHolder()  : string
Saves the placeholder for a string, for later use. This gets around double escaping content in URL's.
_linkEmails()  : string
Links email addresses
_linkUrls()  : string
Replace placeholders with links.

Properties

$_configInitialized

Whether the config property has already been configured with defaults

protected bool $_configInitialized = false

$_defaultConfig

Default config for this class

protected array<string, mixed> $_defaultConfig = ['engine' => \Cake\Utility\Text::class]

$_helperMap

A helper lookup table used to lazy load helper objects.

protected array<string, array<string|int, mixed>> $_helperMap = []

$_placeholders

An array of hashes and their contents.

protected array<string, array<string|int, mixed>> $_placeholders = []

Used when inserting links into text.

$_View

The View instance this helper is attached to

protected View $_View

$helpers

helpers

protected array<string|int, mixed> $helpers = ['Html']

Methods

__call()

Call methods from String utility class

public __call(string $method, array<string|int, mixed> $params) : mixed
Parameters
$method : string

Method to invoke

$params : array<string|int, mixed>

Array of params for the method.

Return values
mixed —

Whatever is returned by called method, or false on failure

__construct()

Constructor

public __construct(View $view[, array<string, mixed> $config = [] ]) : mixed

Settings:

  • engine Class name to use to replace String functionality. The class needs to be placed in the Utility directory.
Parameters
$view : View

the view object the helper is attached to.

$config : array<string, mixed> = []

Settings array Settings array

Tags
throws
CakeException

when the engine class could not be found.

__debugInfo()

Returns an array that can be used to describe the internal state of this object.

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

Lazy loads helpers.

public __get(string $name) : Helper|null|void
Parameters
$name : string

Name of the property being accessed.

Return values
Helper|null|void —

Helper instance if helper with provided name exists

addClass()

Adds the given class to the element options

public addClass(array<string, mixed> $options, string $class[, string $key = 'class' ]) : array<string, mixed>
Parameters
$options : array<string, mixed>

Array options/attributes to add a class to

$class : string

The class name being added.

$key : string = 'class'

the key to use for class. Defaults to 'class'.

Return values
array<string, mixed> —

Array of options with $key set.

autoLinkEmails()

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

public autoLinkEmails(string $text[, array<string, mixed> $options = [] ]) : string

Options

  • escape Control HTML escaping of input. Defaults to true.
Parameters
$text : string

Text

$options : array<string, mixed> = []

Array of HTML options, and options listed above.

Tags
link
https://book.cakephp.org/4/en/views/helpers/text.html#linking-email-addresses
Return values
string —

The text with links

autoLinkUrls()

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

public autoLinkUrls(string $text[, array<string, mixed> $options = [] ]) : string

Options

  • escape Control HTML escaping of input. Defaults to true.
Parameters
$text : string

Text

$options : array<string, mixed> = []

Array of HTML options, and options listed above.

Tags
link
https://book.cakephp.org/4/en/views/helpers/text.html#linking-urls
Return values
string —

The text with links

configShallow()

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

public configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this

Setting a specific value:

$this->configShallow('key', $value);

Setting a nested value:

$this->configShallow('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

Return values
$this

excerpt()

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

public excerpt(string $text, string $phrase[, int $radius = 100 ][, string $ending = '...' ]) : string
Parameters
$text : string

String to search the phrase in

$phrase : string

Phrase that will be searched for

$radius : int = 100

The amount of characters that will be returned on each side of the founded phrase

$ending : string = '...'

Ending that will be appended

Tags
see
Text::excerpt()
link
https://book.cakephp.org/4/en/views/helpers/text.html#extracting-an-excerpt
Return values
string —

Modified string

getConfig()

Returns the config.

public getConfig([string|null $key = null ][, mixed $default = null ]) : mixed

Usage

Reading the whole config:

$this->getConfig();

Reading a specific value:

$this->getConfig('key');

Reading a nested value:

$this->getConfig('some.nested.key');

Reading with default value:

$this->getConfig('some-key', 'default-value');
Parameters
$key : string|null = null

The key to get or null for the whole config.

$default : mixed = null

The return value when the key does not exist.

Return values
mixed —

Configuration data at the named key or null if the key does not exist.

getConfigOrFail()

Returns the config for this specific key.

public getConfigOrFail(string $key) : mixed

The config value for this key must exist, it can never be null.

Parameters
$key : string

The key to get.

Tags
throws
InvalidArgumentException
Return values
mixed —

Configuration data at the named key

getView()

Get the view instance this helper is bound to.

public getView() : View
Return values
View —

The bound view instance.

highlight()

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.

public highlight(string $text, string $phrase[, array<string, mixed> $options = [] ]) : string
Parameters
$text : string

Text to search the phrase in

$phrase : string

The phrase that will be searched

$options : array<string, mixed> = []

An array of HTML attributes and options.

Tags
see
Text::highlight()
link
https://book.cakephp.org/4/en/views/helpers/text.html#highlighting-substrings
Return values
string —

The highlighted text

implementedEvents()

Event listeners.

public implementedEvents() : array<string, mixed>
Return values
array<string, mixed>

initialize()

Constructor hook method.

public initialize(array<string, mixed> $config) : void

Implement this method to avoid having to overwrite the constructor and call parent.

Parameters
$config : array<string, mixed>

The configuration settings provided to this helper.

setConfig()

Sets the config.

public setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this

Usage

Setting a specific value:

$this->setConfig('key', $value);

Setting a nested value:

$this->setConfig('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

$merge : bool = true

Whether to recursively merge or overwrite existing config, defaults to true.

Tags
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

slug()

Returns a string with all spaces converted to dashes (by default), characters transliterated to ASCII characters, and non word characters removed.

public slug(string $string[, array<string, mixed>|string $options = [] ]) : string

Options:

  • replacement: Replacement string. Default '-'.
  • transliteratorId: A valid transliterator id string. If null (default) the transliterator (identifier) set via Text::setTransliteratorId() or Text::setTransliterator() will be used. If false no transliteration will be done, only non-words will be removed.
  • preserve: Specific non-word character to preserve. Default null. For e.g. this option can be set to '.' to generate clean file names.
Parameters
$string : string

the string you want to slug

$options : array<string, mixed>|string = []

If string it will be used as replacement character or an array of options.

Tags
see
Text::setTransliterator()
see
Text::setTransliteratorId()
Return values
string

tail()

Truncates text starting from the end.

public tail(string $text[, int $length = 100 ][, array<string, mixed> $options = [] ]) : string

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
$text : string

String to truncate.

$length : int = 100

Length of returned string, including ellipsis.

$options : array<string, mixed> = []

An array of HTML attributes and options.

Tags
see
Text::tail()
link
https://book.cakephp.org/4/en/views/helpers/text.html#truncating-the-tail-of-a-string
Return values
string —

Trimmed string.

toList()

Creates a comma separated list where the last two items are joined with 'and', forming natural language.

public toList(array<string|int, string> $list[, string|null $and = null ][, string $separator = ', ' ]) : string
Parameters
$list : array<string|int, string>

The list to be joined.

$and : string|null = null

The word used to join the last and second last items together with. Defaults to 'and'.

$separator : string = ', '

The separator used to join all the other items together. Defaults to ', '.

Tags
see
Text::toList()
link
https://book.cakephp.org/4/en/views/helpers/text.html#converting-an-array-to-sentence-form
Return values
string —

The glued together string.

truncate()

Truncates text.

public truncate(string $text[, int $length = 100 ][, array<string, mixed> $options = [] ]) : string

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
Parameters
$text : string

String to truncate.

$length : int = 100

Length of returned string, including ellipsis.

$options : array<string, mixed> = []

An array of HTML attributes and options.

Tags
see
Text::truncate()
link
https://book.cakephp.org/4/en/views/helpers/text.html#truncating-text
Return values
string —

Trimmed string.

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_configRead()

Reads a config key.

protected _configRead(string|null $key) : mixed
Parameters
$key : string|null

Key to read.

_configWrite()

Writes a config key.

protected _configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
$key : array<string, mixed>|string

Key to write to.

$value : mixed

Value to write.

$merge : string|bool = false

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Tags
throws
CakeException

if attempting to clobber existing config

_confirm()

Returns a string to be used as onclick handler for confirm dialogs.

protected _confirm(string $okCode, string $cancelCode) : string
Parameters
$okCode : string

Code to be executed after user chose 'OK'

$cancelCode : string

Code to be executed after user chose 'Cancel'

Return values
string —

"onclick" JS code

_insertPlaceHolder()

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

protected _insertPlaceHolder(array<string|int, mixed> $matches) : string
Parameters
$matches : array<string|int, mixed>

An array of regexp matches.

Return values
string —

Replaced values.

_linkEmails()

Links email addresses

protected _linkEmails(string $text, array<string, mixed> $options) : string
Parameters
$text : string

The text to operate on

$options : array<string, mixed>

An array of options to use for the HTML.

Tags
see
TextHelper::autoLinkEmails()
Return values
string

_linkUrls()

Replace placeholders with links.

protected _linkUrls(string $text, array<string, mixed> $htmlOptions) : string
Parameters
$text : string

The text to operate on.

$htmlOptions : array<string, mixed>

The options for the generated links.

Return values
string —

The text with links inserted.


        
On this page

Search results