Clement Faria - Barbados Website Multimedia Developer


By Clement Faria   Published: February 6, 2013  

This is just another reason why php is my programming language of choice. I was developing a website for a client which was using a cms system, and they wanted to display news on the home page. Displaying the news was no problem, how ever, I wanted to limit the amount of text to be displayed for the description.

To accomplish this I found some useful php code that allowed me to limit the amount of characters in a string using the strlen and substr function. The strlen function will basically get the string's lenght, or number of characters while the substr function will shorten the string to th amount I specify. A lovely piece of code indeed, here is the code below:


// Limit $description to the amount characters you want shortened
$limit = 30;

// description text
$description = "This is where the Description Text would go";

// check to see if description text is more than$limit
if (strlen($description> $limit)

// shorten description text and add "..." to the end
$description = substr($description0, strrpos(substr($description0, $limit)' ')) '...';

// print out the shortened description
echo $description;



Running that php code there will shorten the text in the $description variable and output the following result:

This is where the De...


This snippet can be used for many things. It is useful for shortening cms or blog news posts, that's what I used it for. Feel free to share this page and use this code in your work.

« Back

Hide Comments (0)

Sorry, your account does not have access to post comments.