How to limit text with or without the Joomla Framework

We’ll start with Joomla. If you need to limit a string to a specified character count then why not use Joomla’s built in functions.  The advantage of using Joomla’s truncate function over standard PHP functions is that Joomla will not cut up words – it keeps words whole, only limiting the block of text at a space closest to the limit specified.

How to use Joomla Truncate()

echo JHTML::_('string.truncate', $text_to_truncate, $character_limit);

Note that the function also adds three dots (…) to the end of the sentence – if you’re not a fan of this, see below…

How to truncate and preserve words outside of Joomla

Of course, you might not be using Joomla and need to limit characters without cutting up words.  In which case, here is a function for doing so:

function truncate($text, $length = 0, $end='...')
{
// Truncate the item text if it is too long.
if ($length > 0 && strlen($text) > $length)
{
// Find the first space within the allowed length.
$tmp = substr($text, 0, $length);
$tmp = substr($tmp, 0, strrpos($tmp, ' '));

// If we don't have 3 characters of room, go to the second space within the limit.
if (strlen($tmp) >= $length - 3) {
$tmp = substr($tmp, 0, strrpos($tmp, ' '));
}

$text = $tmp.$end;
}
return $text;
}

It’s the function Joomla uses, with built-in Joomla functions swapped out for standard PHP functions – so it can be used in any PHP application!  Thanks to the Joomla development team for this one.

I’ve also added in an optional third parameter that allows you to specify the text at the end.  The default is set to ‘…’, but you can pass in anything you like, even a read more link:

echo truncate ($text_to_truncate, 100, " - <a href='http://www.samontheweb.co.uk' target='_blank'>Read more</a>");

The above will output your text, limited to 100 characters (without splitting up words in the process) and give a ‘Read more’ hyperlink at the end.

If you have any questions or need further information, don’t hesitate to leave a comment below!

2 Responses to How to limit text with or without the Joomla Framework
  1. Christopher Reply

    Dear Sam,

    thank you very much for your post; I found it by Googling for a good truncate function and I was glad I found it build-in to Joomla!

    There is one thing I would like to add though which you miss in your article – Joomla’s truncate function is quite a bit more complex than your second function. It does correctly deal with any HTML Tags in the String, which is tremendously useful. Basically, it makes sure there are no un-closed tags like p, h2, h3, b, em etc.

    So if anyone wants that functionality in their own function, download a recent Joomla Installation and have a look at it. You can find the function in /libraries/cms/html/strings.php

    best wishes
    Chris

  2. stephen Reply

    need 100 words limit to be applied in joomla home page feature post (columns) and to come inform of excerpt with image either on top, left or right of the texts, 300 words limit text in blog post but that of the article post can come in form of full lenght. how do i go about this? how do i go about it so that image for the column except post can come in dimension of 100/80px, image for the blog 200/150px and image for article post can come inform of 400/250px just like in the case of most wordpress theme

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.