Skip to content
This is the documentation for pieceofcake2/cakephp, a community-maintained fork of CakePHP 2.x for PHP 8. About this fork.

CakeText ​

class CakeText

The CakeText class includes convenience methods for creating and manipulating strings and is normally accessed statically. Example: CakeText::uuid().

Piece of Cake 2 fork

The String class, deprecated since 2.7, has been removed because String is a reserved word since PHP 7. Replace String:: calls with CakeText::.

If you need TextHelper functionalities outside of a View, use the CakeText class:

class UsersController extends AppController
{
    public $components = ['Auth'];

    public function afterLogin()
    {
        $message = $this->User->find('new_message');
        if (!empty($message)) {
            // notify user of new message
            $this->Session->setFlash(__('You have a new message: %s', CakeText::truncate($message['Message']['body'], 255, ['html' => true])));
        }
    }
}

Changed in version 2.1

Several methods from TextHelper have been moved to CakeText class.

static CakeText::uuid()

The UUID method is used to generate unique identifiers as per 4122. The UUID is a 128bit string in the format of 485fc381-e790-47a3-9794-1337c0a8fe68.

CakeText::uuid(); // 485fc381-e790-47a3-9794-1337c0a8fe68

static CakeText::tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')')

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

This method can be useful when splitting up data in that has regular formatting such as tag lists:

$data = "cakephp 'great framework' php";
$result = CakeText::tokenize($data, ' ', "'", "'");
// result contains
['cakephp', "'great framework'", 'php'];

static CakeText::insert($string, $data, $options = [])

The insert method is used to create string templates and to allow for key/value replacements:

CakeText::insert('My name is :name and I am :age years old.', ['name' => 'Bob', 'age' => '65']);
// generates: "My name is Bob and I am 65 years old."

static CakeText::cleanInsert($string, $options = [])

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

You can use the following options in the options array:

$options = [
    'clean' => [
        'method' => 'text', // or html
    ],
    'before' => '',
    'after' => '',
];

static CakeText::wrap($text, $options = [])

Wraps a block of text to a set width, and indent blocks as well. Can intelligently wrap text so words are not sliced across lines:

$text = 'This is the song that never ends.';
$result = CakeText::wrap($text, 22);

// returns
This is the song
that never ends.

You can provide an array of options that control how wrapping is done. The supported options are:

  • width The width to wrap to. Defaults to 72.
  • wordWrap Whether or not to wrap whole words. Defaults to true.
  • indent The character to indent lines with. Defaults to ''.
  • indentAt The line number to start indenting text. Defaults to 0.

method CakeText::highlight(string $haystack, string $needle, array $options = [] )

method CakeText::stripLinks($text)

method CakeText::truncate(string $text, int $length=100, array $options)

Changed in version 2.3

ending has been replaced by ellipsis. ending is still used in 2.2.1

method CakeText::tail(string $text, int $length=100, array $options)

method CakeText::excerpt(string $haystack, string $needle, integer $radius=100, string $ellipsis="...")

method CakeText::toList(array $list, $and='and')

Based on the CakePHP Cookbook. Content licensed under CC BY-NC-SA 4.0. Not affiliated with the Cake Software Foundation.