Follow @endyourif rss Facebook LinkedIn

Using the TextHelper with CakePHP Using the TextHelper with CakePHP

Published on Jan 24, 2011

Really, the TextHelper?  Yes, this is probably one of the most overlooked helpers in CakePHP.  Sure we all know about the HTML Helper, Form Helper, JS Helper, etc... but how many of us use the Text Helper?  I've seen so many custom functions for truncating text and adding an ellipsis (...) to the end of it.  How about replacing email addresses with links?  Etc...  No more I say!  Let's begin explorining the Text Helper now.

First we begin by importing it in to our code.  Open a controller that controls your view:


class MyController extends AppController {


var $helpers = array('Text');




Now, let's open up our view and begin using it:

Email: <?php echo $this->Text->autoLinkEmails($my_email);?><br/>

Website: <?php echo $this->Text->autoLinkUrls($my_url);?><br/>

Company Description:<br/>

<?php echo $this->Text->autoLink($my_desc);?>

Above are three different scenarios all doing very similar things.  The first example, will convert our normal email address into a "mailto" link allowing users one-click access to send an email.  The second example, will turn our website into a clickable link.  Finally, the third example will convert both an email and/or a URL into a link!

Now you don't need to write crazy regular expressions looking for what might be the start of a link and the end of link; it's already done for you.  Not only that, it has been well tested!

Next we have the "truncate" function.  Do you have a news listing where you want to show a brief description of the article?  See the sample below to help you get it done quickly:


<?php foreach ($articles as $article):?>


<div><?php echo $article['title'];?></div>


<?php echo $this->Text->truncate($article['description'], 250, array('ending' => '...', 'exact' => false));?>



<?php endforeach;?>

Not done yet, the last example I want to touch upon is the toList function; another excellent time saver.  In this example, let's assume that we have a user profile and we have them enter their interests in.  In today's society this would most likely be done with some sort of auto complete functionality to avoid the user having typos and to speed things up.  We could then store a comma separated list of this interests in the users table.  Then when we want to display what the user is interested in, we use this handy function:

Interests: <?php echo $this->Text->toList(explode(',', $user['interests']));?>

Our comma separated list remains the same, but CakePHP has automatically added ", and " to our last interest automatically.

A few other great functions that I will allow you to explore further are the following:

- highlight

- excerpt

- stripLinks

Visit the Cookbook for full details:


Tags: CakePHP Tutorial | helpers

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP