Shorten Paragraph or String without broken/incomplete word using PHP up to a maximum character length

Ever wondered how to shorten a paragraph or string using PHP without broken words? There is a function in PHP substr() which cops a string upto a character length. But the problem is, that it leaves incomplete words after shortening.

Say you have a paragraph:

The famous astrophysicist, speaking in a new documentary, said spaceships could one day be capable of such high speeds that time slowed down for those on board.

According to the Times of London, Hawking has made a new documentary, “Stephen Hawking’s Universe,” for the Discovery Channel, which is scheduled to be broadcast in May. In this film, which took three years to make, Hawking offers his view of what really might be out there.Hawking says in his new documentary series ,how man might truly conquer space.

You want to chop it to 100 character length. So normally it would become

The famous astrophysicist, speaking in a new documentary, said spaceships could one day be capable o

Obviously the character o remains as broken word, which does not look good at all. In this tutorial we shall see, how to properly shorten a string without leaving incomplete words and adding a continuation … sign after it!

For the savvy users, here is the Online Demo and Download Link:

The code becomes handy while producing excerpts from long paragraphs. If you want to know the inside of this code, then read on!

Strategy behind the Code:

We shall use the following strategy before coding the PHP script:

  • First we will chop the given string to the given character using substr() function;
  • Now we will use strrpos() function to check if we again chop the string upto the last occurrence of a space ‘ ‘ then whether the string becomes characterless (‘’). If it does not become characterless then we will chop it upto the last occurrence of a space character.
  • Finally we will add … sign to the end of the string using PHP’s concatenation function. (A simple dot eh?)
  • And we return the string from the function for further use

The code:

Keeping all the strategies in mind, below is the function which will do our job!

function string_shorten($text, $char) {
    $text = substr($text, 0, $char); //First chop the string to the given character length
    if(substr($text, 0, strrpos($text, ' '))!='') $text = substr($text, 0, strrpos($text, ' ')); //If there exists any space just before the end of the chopped string take upto that portion only.
    //In this way we remove any incomplete word from the paragraph
    $text = $text.'...'; //Add continuation ... sign
    return $text; //Return the value
}

Everything is commented there for better understanding. Now we call the function simply like this:

$string = <<<EOD
The famous astrophysicist, speaking in a new documentary, said spaceships could one day be capable of such high speeds that time slowed down for those on board.
According to the Times of London, Hawking has made a new documentary, "Stephen Hawking's Universe," for the Discovery Channel, which is scheduled to be broadcast in May. In this film, which took three years to make, Hawking offers his view of what really might be out there.Hawking says in his new documentary series ,how man might truly conquer space.
EOD;
$number = 100;
$short = string_shorten($string, $number);
echo $short;

For more information, have a look at the downloadable package which contains all the codes I have used in the Online Demonstration… Below are the links:


Well, that’s all. I hope it was useful for you! Do give us your feedback! If you face any problem, then feel free to ask here…

1 Comment

Comments are closed.