Home > PHP Strings > Cut A String To A Specified Length With PHP

Cut A String To A Specified Length With PHP

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.

$string = '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.

function substrwords($text,$maxchar,$end='...'){
 if(strlen($text)>$maxchar){
  $words=explode(" ",$text);
  $output = '';
  $i=0;
  while(1){
   $length = (strlen($output)+strlen($words[$i]));
   if($length>$maxchar){
    break;
   }else{
    $output = $output." ".$words[$i];
    ++$i;
   };
  };
 }else{
  $output = $text;
 }
 return $output.$end;
}

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...

Categories: PHP Strings Tags: , ,
  1. DV
    August 12th, 2008 at 11:54 | #1

    Great! Really useful! I was looking for that!

  2. Adrian
    August 27th, 2008 at 20:49 | #2

    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!

  3. March 25th, 2009 at 04:48 | #3

    Its useful. thanks so much.

  4. August 12th, 2009 at 14:01 | #4

    I just put this code on my website 3was.com to shrink the strings in my alt tag on the pictures. Works great! Thank you

  5. February 24th, 2010 at 23:42 | #5

    Loved it, nice work!

  6. February 26th, 2010 at 15:27 | #6

    I think wordwrap function does exactly the same. PHP has so many functions… cheers

  7. February 26th, 2010 at 15:29 | #7
  8. David
    May 4th, 2011 at 11:50 | #8

    Very usefull. If you don’t want trailing dots after a string that is shorter than the maximum length specified, use this slightly altered version:

    function substrwords($text,$maxchar,$end=’…’){
    if(strlen($text)>$maxchar){
    $words=explode(” “,$text);
    $output = ”;
    $i=0;
    while(1){
    $length = (strlen($output)+strlen($words[$i]));
    if($length>$maxchar){
    $output = $output.$end;
    break;
    }else{
    $output = $output.” “.$words[$i];
    ++$i;
    };
    };
    }else{
    $output = $text;
    }
    return $output;
    }

  1. No trackbacks yet.