Cut A String To A Specified Length With PHP

17th April 2008

Cutting a string to a specified length is accomplished with the substr() function. For example, the following string variable, which we will cut to a maximum of 30 characters.

$string = 'This string is too long and will be cut short.';

The substr() function has three parameters. The first is the string, the second is where to start cutting from and the third is the amount of characters to cut to. So to cut to 30 characters we would use the following.

$string = substr($string,0,30);

The string variable is now set to the following.

This string is too long and wi

This example highlights the major problem with this function in that it will take no notice of the words in a string. The solution is to create a new function that will avoid cutting words apart when cutting a string by a number of characters.

  1. function substrwords($text, $maxchar, $end='...') {
  2. if (strlen($text) > $maxchar || $text == '') {
  3. $words = preg_split('/\s/', $text);
  4. $output = '';
  5. $i      = 0;
  6. while (1) {
  7. $length = strlen($output)+strlen($words[$i]);
  8. if ($length > $maxchar) {
  9. break;
  10. }
  11. else {
  12. $output .= " " . $words[$i];
  13. ++$i;
  14. }
  15. }
  16. $output .= $end;
  17. }
  18. else {
  19. $output = $text;
  20. }
  21. return $output;
  22. }

This function splits the string into words and then joins them back together again one by one. The good thing is that if there is a word that would make the string longer than the required number of characters it doesn't include the word. The function can be called like this.

echo substrwords($string,30);

This produces the following output.

This string is too long and...

 

Comments

Permalink
Great! Really useful! I was looking for that!

DV (Tue, 08/12/2008 - 11:54)

Permalink
Hah! Exactly what I was looking for, thanks a bunch..! ;) By the way: this thing is rating pretty high on Google's search results; keep up the good work!

Adrian (Wed, 08/27/2008 - 20:49)

Permalink
Its useful. thanks so much.

Viet Trang (Wed, 03/25/2009 - 04:48)

Permalink

Save a bit of processor time by doing this:

$length = 0; while ($length

Shogobg (Wed, 07/13/2011 - 22:42)

Permalink
Good. very Useful though It gives error when the string is empty.

Vikas Kumar (Tue, 10/22/2013 - 11:24)

Permalink
Really work wonderfully gooooooooooood!!! you are a guru philip

Kay (Mon, 10/24/2016 - 01:35)

Permalink
Still works after all these years. Thanks for the helpful tip!

Kade (Wed, 02/01/2017 - 02:01)

Permalink
Something like this? function truncate_text($body, $max_length) { if (strlen($body) > $max_length) { return substr($body, 0, strrpos(substr($body, 0, $max_length + 1), ' ')) . '...'; } return $body; } People could also look at php's wordwrap function.

Cameron Green (Sat, 02/18/2017 - 04:21)

Permalink
Great!! It was extremely helpful. This was so easy to follow and exactly what I was looking for. Thanks.

Web Developer Php (Thu, 08/10/2017 - 05:50)

Permalink

Thanks for this, it saved my skin

Kay (Tue, 09/25/2018 - 18:53)

Permalink

Great.! - Still working, tested with PHP7.2

// UTF8 compatibility » add "mb_" before "strlen" for UTF8 compatibility

function substrwords($text, $maxchar, $end=' ...') {
    if (mb_strlen($text) > $maxchar || $text == '') {
        $words = preg_split('/\s/', $text);      
        $output = '';
        $i      = 0;
        while (1) {
            $length = mb_strlen($output)+mb_strlen($words[$i]);
            if ($length > $maxchar) {
                break;
            } 
            else {
                $output .= " " . $words[$i];
                ++$i;
            }
        }
        $output .= $end;
    } 
    else {
        $output = $text;
    }
    return $output;
}
 

Mike Orfanidis (Mon, 11/05/2018 - 12:05)

Permalink

(Y)

Anonymous (Tue, 05/07/2019 - 17:08)

Add new comment

The content of this field is kept private and will not be shown publicly.