How to truncate text in PHP without third-party library

Having a long text as your website content can be a problem because it can break your web page layout.

To truncate (or cut short) a long text using PHP, you need to:

This tutorial will help you learn how to do the steps above. Let’s begin.

Step #1 - Use the substr() function to truncate the text

The substr() function is used to extract a part of the string.

This function can be used to truncate a text as shown below:

<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

// πŸ‘‡ use substr to get the first 20 characters
$truncated_text = substr($text, 0, 20);

print $truncated_text; // Lorem ipsum dolor si

In the example above, the function is used to get the string from index 0 to 20.

Step #2 - Add ellipsis to the truncated text

After you truncate the text, add the ellipsis to let readers know that you omit a portion of the text:

<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

// πŸ‘‡ use substr to get the first 20 characters
$truncated_text = substr($text, 0, 20);

// πŸ‘‡ add ellipsis to the text
$truncated_text .= "...";

print $truncated_text; // Lorem ipsum dolor si...

Finally, you see that the truncation leaves an incomplete word. You can make the truncation clean by cutting the text at the last space character.

Step #3 - Truncate text at the last complete word

You can find the last occurrence of space character using the strrpos() function:

<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

// πŸ‘‡ use substr to get the first 20 characters
$truncated_text = substr($text, 0, 20);

// πŸ‘‡ find the last space character index
$last_space = strrpos($truncated_text, " ");

// πŸ‘‡ truncate the text again, to the last space character
$truncated_text = substr($truncated_text, 0, $last_space);

// πŸ‘‡ add ellipsis to the text
$truncated_text .= "...";

print $truncated_text; // Lorem ipsum dolor...

Now the truncation leaves a complete word at the truncation point.

Step #4 - Create a custom function to truncate texts

And that’s all you need to truncate a text using PHP. You can create a custom function to store the code as follows:

/**
 * A function to truncate a text
 *
 *
 * @param string $text The text to truncate
 * @param int $length Optional. The length of the new text.
 * @return string a new truncated text.
 */
function truncate(string $text, int $length = 20): string {
    if (strlen($text) <= $length) {
        return $text;
    }
    $text = substr($text, 0, $length);
    $text = substr($text, 0, strrpos($text, " "));
    $text .= "...";
    return $text;
}

The function above also allows you to specify the length of the truncated text.

It defaults to 20 but you can choose any length you want.

Now anytime you need to truncate a text, you just call the truncate() function:

<?php

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

print truncate($text); // Lorem ipsum dolor...
print truncate($text, 25); // Lorem ipsum dolor sit...

Feel free to use the truncate() function in your project.

Now you’ve learned how to truncate a text using PHP. Easy, right? πŸ˜‰

Take your skills to the next level ⚑️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.