CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.0 Red Velvet API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.0
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • I18n
    • Log
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
      • Crypto
      • Exception
    • Validation
    • View

Class Text

Text handling methods.

Namespace: Cake\Utility

Method Summary

  • _wordWrap() protected static

    Unicode aware version of wordwrap as helper method.

  • ascii() public static

    Converts the decimal value of a multibyte character string to a string

  • cleanInsert() public static

    Cleans up a Text::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 Text::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:

    Text::insert(':name is :age years old.', ['name' => 'Bob', '65']);

    Returns: Bob is 65 years old.

  • isMultibyte() public static

    Check if the string contain multibyte characters

  • parseFileSize() public static

    Converts filesize from human readable string to bytes

  • stripLinks() public static

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

  • tail() public static

    Truncates text starting from the end.

  • toList() public static

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

  • tokenize() public static

    Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound and $rightBound.

  • truncate() public static

    Truncates text.

  • utf8() public static

    Converts a multibyte character string to the decimal value of the character

  • uuid() public static

    Generate a random UUID version 4

  • wordWrap() public static

    Unicode and newline aware version of wordwrap.

  • wrap() public static

    Wraps text to a specific width, can optionally wrap at word breaks.

  • wrapBlock() public static

    Wraps a complete block of text to a specific width, can optionally wrap at word breaks.

Method Detail

_wordWrap() ¶ protected static

_wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string

Unicode aware version of wordwrap as helper method.

Parameters
string $text

The text to format.

int $width optional

The width to wrap to. Defaults to 72.

string $break optional

The line is broken using the optional break parameter. Defaults to '\n'.

bool $cut optional

If the cut is set to true, the string is always wrapped at the specified width.

Returns
string

ascii() ¶ public static

ascii(array $array): string

Converts the decimal value of a multibyte character string to a string

Parameters
array $array

Array

Returns
string

cleanInsert() ¶ public static

cleanInsert(string $str, array $options): string

Cleans up a Text::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 Text::insert().

Parameters
string $str

String to clean.

array $options

Options list.

Returns
string
See Also
\Cake\Utility\Text::insert()

excerpt() ¶ public static

excerpt(string $text, string $phrase, int $radius = 100, string $ellipsis = '...'): string

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

int $radius optional

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

string $ellipsis optional

Ending that will be appended

Returns
string
Links
http://book.cakephp.org/3.0/en/core-libraries/string.html#extracting-an-excerpt

highlight() ¶ public static

highlight(string $text, string|array $phrase, array $options = []): 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.

Options:

  • format The piece of HTML with that the phrase will be highlighted
  • html If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
  • regex a custom regex rule that is used to match words, default is '|$tag|iu'
Parameters
string $text

Text to search the phrase in.

string|array $phrase

The phrase or phrases that will be searched.

array $options optional

An array of HTML attributes and options.

Returns
string
Links
http://book.cakephp.org/3.0/en/core-libraries/string.html#highlighting-substrings

insert() ¶ public static

insert(string $str, array $data, array $options = []): string

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:

Text::insert(':name is :age years old.', ['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 Text::cleanInsert
Parameters
string $str

A string containing variable placeholders

array $data

A key => val array where each key stands for a placeholder variable name to be replaced with val

array $options optional

An array of options, see description above

Returns
string

isMultibyte() ¶ public static

isMultibyte(string $string): bool

Check if the string contain multibyte characters

Parameters
string $string

value to test

Returns
bool

parseFileSize() ¶ public static

parseFileSize(string $size, mixed $default = false): mixed

Converts filesize from human readable string to bytes

Parameters
string $size

Size in human readable string like '5MB', '5M', '500B', '50kb' etc.

mixed $default optional

Value to be returned when invalid size was used, for example 'Unknown type'

Returns
mixed
Throws
InvalidArgumentException
On invalid Unit type.
Links
http://book.cakephp.org/3.0/en/core-libraries/helpers/text.html

stripLinks() ¶ public static

stripLinks(string $text): string

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

Parameters
string $text

Text

Returns
string

tail() ¶ public static

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

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.

int $length optional

Length of returned string, including ellipsis.

array $options optional

An array of options.

Returns
string

toList() ¶ public static

toList(array $list, string $and = null, string $separator = ', '): string

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

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
Links
http://book.cakephp.org/3.0/en/core-libraries/string.html#converting-an-array-to-sentence-form

tokenize() ¶ public static

tokenize(string $data, string $separator = ',', string $leftBound = '(', string $rightBound = ')'): mixed

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
mixed

truncate() ¶ public static

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

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
  • 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.

int $length optional

Length of returned string, including ellipsis.

array $options optional

An array of HTML attributes and options.

Returns
string
Links
http://book.cakephp.org/3.0/en/core-libraries/string.html#truncating-text

utf8() ¶ public static

utf8(string $string): array

Converts a multibyte character string to the decimal value of the character

Parameters
string $string

String to convert.

Returns
array

uuid() ¶ public static

uuid(): string

Generate a random UUID version 4

Warning: This method should not be used as a random seed for any cryptographic operations. Instead you should use the openssl or mcrypt extensions.

Returns
string
See Also
http://www.ietf.org/rfc/rfc4122.txt

wordWrap() ¶ public static

wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string

Unicode and newline aware version of wordwrap.

Parameters
string $text

The text to format.

int $width optional

The width to wrap to. Defaults to 72.

string $break optional

The line is broken using the optional break parameter. Defaults to '\n'.

bool $cut optional

If the cut is set to true, the string is always wrapped at the specified width.

Returns
string

wrap() ¶ public static

wrap(string $text, array|int $options = []): string

Wraps text to a specific width, can optionally wrap at word breaks.

Options

  • width The width to wrap to. Defaults to 72.
  • wordWrap 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

The text to format.

array|int $options optional

Array of options to use, or an integer to wrap the text to.

Returns
string

wrapBlock() ¶ public static

wrapBlock(string $text, array|int $options = []): string

Wraps a complete block of text to a specific width, can optionally wrap at word breaks.

Options

  • width The width to wrap to. Defaults to 72.
  • wordWrap 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

The text to format.

array|int $options optional

Array of options to use, or an integer to wrap the text to.

Returns
string
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs