Click here to Skip to main content
11,480,938 members (63,444 online)
Click here to Skip to main content

Tagged as

Truncate to max characters without breaking word in between

, 19 Dec 2012 CPOL 3.5K 3
Rate this:
Please Sign up or sign in to vote.
A handy method to truncate words to a certain number of characters without breaking any word in between

Introduction

Very often we want to display only a chunk of the large text present in database in the front end. For this, we generally split the words to a certain number of characters and display them.

For ex- , if we want to display only 15 characters in the front end and we have a text as "Hello World, This is a very long line of text that needs to be truncated to 15 characters!!", we will take the 1st 15 characters of the text and display the same. This would give an output as "Hello World, Thi". Obviously, this look pretty ugly and not much user-friendly for the users visiting any site.

This tip is helpful to resolve similar kind of situation. Users can use the below function when they need to truncate a long text to a certain number of characters without breaking the word in between.

Using the code 

Define the below function in the file you want to use it. In case you want it to use in multiple files, you can always define it one of your core-level files that is included in each file you want this to be used.  

function truncate_str($str, $maxlen) {
    if (strlen($str) <= $maxlen) {
       return $str;
    }  
    $newstr = substr($str, 0, $maxlen);
    if ( substr($newstr,-1,1) != ' ' ) 
        $newstr = substr($newstr, 0, strrpos($newstr, " "));
    return $newstr;
}

This function requires two parameters:

  1. Original text to be truncated
  2. Maximum number of characters the text should be truncated to.

Calling the truncate_str() function as below will give you the output as "Hello World, " -- thus displaying neat  texts without displaying half words.

echo truncate_str("Hello World, This is a very long line of text that will 
         be truncated to less than 15 characters without breaking the words in between!!", 15);

Hope this little tip helps you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.

I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Dec 2012
Article Copyright 2012 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid