Useful string manipulation helpers in CakePHP

CakePHP comes with a bunch of useful helpers and components which provide handy tools for processing and manipulating data. These helpers are not only the valuable assets for the a framework user, but can be of great help to someone writing a core php application and not using the CakePHP framework.

Here are a few examples:

Number Helper

Number helper provides useful function to manipulate numbers.

precision()

precision() returns a number formatted with a level of precision. For example:

echo $number->precision(25.02558, 3);
//outputs
25.026

toReadableSize()

This function takes filesize as a argument and return the well formatted string mentioning the size in terms of bytes, KB, MB, GB and TB. For example:

echo $number->toReadableSize(2502558);
//outputs
2.39 MB

Other function in number helper are:
toPercentage(): Formats a number into a percentage string.
format():Formats a number into a currency format.
currency():Formats a number into a currency format labeled with currency sign.

See cake/libs/view/helpers/number.php for a complete list of number helper functions and their usage guidelines.

TextHelper

TextHelper provides useful function to manipulate text strings.

highlight()

Highlights a given phrase in a text. You can specify any expression in the third argument that may include the \1 expression to include the $phrase(second argument) found in the $text(first argument). For example:

echo $text->highlight("Word example will be highlighted","example");
//outputs

This is the string in which word example will be highlighted

Note: you must have to define a css style for “highlight” in your css stylesheet. for example:

.highlight{background:#FFFF00;}

stripLinks()

Strips all links (< href=….) from given text. Example:

echo $text->stripLinks('Dont show the clickable link on Online Charity Mall');
//outputs
Dont show the clickable link on Online Charity Mall

autoLinkUrls()

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

autoLinkEmails()

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

truncate()

Cuts a string to the length of given second argument and replaces the last characters. Takes care of HTML tags and encoding.

For example:

echo $text->truncate("Cuts a string to the length of given second argument and replaces the last characters. Takes care of HTML tags and encoding.",50);
//outputs
Cuts a string to the length of given second arg...

excerpt()

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

toList()

Creates a comma separated list where the last two items are joined with ‘and’, forming natural English.

See cake/libs/view/helpers/text.php for a complete list of text helper functions and their usage guidelines.

Time Helper

convert()

Converts given time (in server’s time zone) to user’s local time, given his/her offset from GMT.

nice()

Returns a nicely formatted date string for given Datetime string.

niceShort()

Returns a formatted descriptive date string for given datetime string.

isToday()

Returns true if given datetime string is today.

isThisWeek()

Returns true if given datetime string is within this week.

isThisMonth()

Returns true if given datetime string is within this month.

isThisYear()

Returns true if given datetime string is within this month.

wasYesterday()

Returns true if given datetime string was yesterday.

isTomorrow()

Returns true if given datetime string is tomorrow.

timeAgoInWords()

Returns either a relative date or a formatted date depending on the difference between the current time and given datetime.

format()

Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.

See cake/libs/view/helpers/time.php for a complete list of time helper functions and their usage guidelines. Additionally, there are other helpers some of which are (for example html, form, javascript, ajax) the backbones of cakephp view section. One must look into them inside cake/libs/view/helpers for the best possible usage of them.

Leave a Reply