How to Cut Text, String, And Sentence Using PHP

Sometimes we need to cut a long text or sentence, for example for a fairly long article we need to cut the first 45 words to create an excerpt, there have been many alternative ways to do this, so today we’ll discuss how to cut text, string, or sentence using PHP

Here are some possible conditions and the alternative solutions:

1 Cut The Text Exactly on The Desired Limit

For example, we want to cut a text at a particular position and add three dots to it. The example below will take exactly 45 first letter and added three dots to it.

$text 		= 'This is an example of quite long text or sentence and we want to cut it to use as an excerpt';
$max_char 	= 45;
echo substr($text, 0, $max_char) . '...';

The result is: This is an example of quite long text or sent…

2 Cut text, string, or sentence without truncate the word (eliminate the truncated word)

Continuing the previous example, now we cut first 45 letter BUT without cutting the word, the truncated word will be removed from the result. so we’ll like to do the following:

  1. Find the 45th character of a text or sentence.
  2. If it is a space character, then cut at that position (first 45 letter).
  3. If not a space character, find the previous letter until finding a space, then cut at that position.

This can be achieved through several ways:

Script #1

$text 		= 'This is an example of quite long text or sentence and we want to cut it to use as an excerpt';
$max_char 	= 45;

$cut_text 	= substr($text, 0, $max_char);
if ($text{$max_char - 1} != ' ') { // if the 45th character not a space
	$new_pos 	= strrpos($cut_text, ' '); // find the space from the last character
	$cut_text 	= substr($text, 0, $new_pos);
}
echo $cut_text . '...';

Script #2

$char     = $text{$max_char - 1};
while($char != ' ') {
	$char = $text{--$max_char}; // Find a space from the 49th, 48th, 47th character, etc
}
echo substr($text, 0, $max_char) . '...';

Both produce the same result: This is an example of quite long text or…

The second script more simple and more readable, and after do some test, the second script that uses a “string index” ( $text{--$max_char} ) has execution time 3x faster compared to the first script cheap cialis 5mg.

3 Cut text, string, or sentence without truncate the word (add the truncated word to the result)

Same as the previous example, we’ll cut first 45 letter without cutting the word, but the truncated word will be added to the result. we’ll like to do these following steps:

  1. Find the 45th character of a text or sentence.
  2. If it is a space character, then cut at that position (first 45 letter).
  3. If not space, find the next letter until finding a space, then cut at that position.

This also can be achieved through several ways:

Script #1

if ($text{$max_char - 1} != ' ') {
	$max_char = strpos($text, ' ', $max_char); // Find the space character from the 45th chacter
}
echo substr($text, 0, $max_char) . '...';

Script #2

$char     = $text{$max_char - 1};
while($char != ' ') {
	$char = $text{++$max_char}; // Find the space from 46th, 47th, 48th character, etc...
}
echo substr($text, 0, $max_char) . '...';

The result is same: This is an example of quite long text or sentence…

The second script more readable / understandable, and after do some test, the second script that uses a “string index” ( $text{++$max_char} ) has execution time 5x faster than the first script.

4 Creating Function to Cut Text, String, or Sentence Using PHP

All three conditions above can be gathered into one function so that the code is more organized and easier to use. This is example of function to cut the text or sentences using PHP:

function cutText($text, $max_char, $mode = 2)
{
	if ($mode == 1) {
		return substr($text, 0, $max_char);
	}

	$char = $text{$max_char - 1};
	switch($mode)
	{
		case 2: 
			while($char != ' ') {
				$char = $text{--$max_char};
			}
		case 3:
			while($char != ' ') {
				$char = $text{++$max_char};
			}
	}
	return substr($text, 0, $max_char);
}

The usage example:

$text = 'This is an example of quite long text or sentence and we want to cut it to use as an excerpt';
echo cutText($text, 45, 1) . '...'; // This is an example of quite long text or sent...
echo cutText($text, 45) . '...'; // This is an example of quite long text or...
echo cutText($text, 45, 3) . '...'; // This is an example of quite long text or sentence...

Subscibe Now

Loves articles on webdevzoom.com? join our newsletter to get quality article right to your inbox. Nothing else, just quality stuff!!!

Leave a comment

Like Us

Newsletter

Great information from webdevzoom.com right to your inbox

We value your privacy

Social