PHP

How to break long paragraph using PHP

The text on a screen is harder to read than text in a book but short paragraphs keep content readable. In this tutorial I’ll explain how to convert or split a long text / paragraph into short paragraphs using PHP. I’ll also explain how to use strlen, substr, substr_replace and explode functions.

This function requires three parameters $text, $length, and $tolerance.

  1. $text : the String to be processed.
  2. $length optional : the minimum number of characters in a paragraph.
  3. $maxLength optional : the maximum number of characters in a paragraph.

This function will return an array filled with short paragraphs.

Split long text with substr and substr_replace functions

To break text into smaller chunk, we actually need two things “text itself” and the required size (total characters) of a paragraph. A paragraph is a series of sentences, when breaking a large text or paragraph we have to take care that a sentence should not be split into two paragraph.

That’s why I’ll add two arguments for length: $length as minimum paragraph length and $maxLength for maximum paragraph length.

Step 1. Create a function breakLongText.

function breakLongText($text, $length = 200, $maxLength = 250){

breakLongText function accepts three parameters. The text to be processed $text parameter, $length and $maxLength optional parameters with default arguments values 200 and 250 characters respectively.

Step 2. Obtain $text length with strlen function. The strlen function returns the length of the given string. Now compare obtained $textLength with $maxLength, if the $textLength is smaller than $maxLength then we’ll not break or split this text as it is already a short text.

 $textLength = strlen($text);
 $splitText = array();

 if (!($textLength > $maxLength)){
  $splitText[] = $text;
  return $splitText;
 }

This function will always return an array even the if the text is not processed i.e. for already short text. The above code returns the unprocessed short text by adding it into $splitText array.

Step 3. Next, we’ll use strpos function. strpos function find the position of the first occurrence of a substring in a string, it accepts three parameters: haystack, needle and offset. We’ll use long text ($text) as haystack, “.” for needle and the $length for offset.

strpos starts search from the first character if offset not specified. But if specified, search will start this number of characters counted from the beginning of the string. So in our search will start from the $length offset and find “.” after specified $length offset.

Note: We’ll use “.” for needle to guess the completion of a sentence.

 $needle = '.';
 while (strlen($text) > $length){
  $end = strpos($text, $needle, $length);
  if ($end === false){
   //Returns FALSE if the needle (in this case ".") was not found.
   $splitText[] = substr($text,0);
   $text = '';
   break;
  }
  $end++;

Step 4. We’ll also use substr to extract text from the $text. The substr function accepts three parameters : $text, $start and optional $length. substr function returns the portion of string specified by the start and length parameters. If length is not given, the substring starting from $start until the end of the string will be returned.

  $splitText[] = substr($text,0,$end);
  $text = substr_replace($text,'',0,$end);
 }

The substr_replace function replace text within a portion of a string delimited by the start and (optionally) length parameters with the string given in replacement. It accepts four parameters, $text, $replacement, $start and option $length. If $length is not given then it will replace from $start to at the end of $text.

Step 5. Now add the remaining text to array if still exist and return the $splitText array.

 if ($text){
  $splitText[] = substr($text,0);
 }

 return $splitText;
}

breakLongText function : The complete code

function breakLongText($text, $length = 200, $maxLength = 250){
 //Text length
 $textLength = strlen($text);

 //initialize empty array to store split text
 $splitText = array();

 //return without breaking if text is already short
 if (!($textLength > $maxLength)){
  $splitText[] = $text;
  return $splitText;
 }

 //Guess sentence completion
 $needle = '.';

 /*iterate over $text length 
   as substr_replace deleting it*/  
 while (strlen($text) > $length){

  $end = strpos($text, $needle, $length);

  if ($end === false){

   //Returns FALSE if the needle (in this case ".") was not found.
   $splitText[] = substr($text,0);
   $text = '';
   break;

  }

  $end++;
  $splitText[] = substr($text,0,$end);
  $text = substr_replace($text,'',0,$end);

 }
 
 if ($text){
  $splitText[] = substr($text,0);
 }

 return $splitText;

}
by BrainBellupdated
Advertisement: