How to Limit Length of Text in PHP

Updated by Andy Feliciotti

Need to cut a long title or description down to a short excerpt in PHP? This comes up all the time for cards, meta descriptions and previews.

In this post, I’ll show you the quick way, the safer way for non-English text, and a small function that won’t chop a word in half. Let’s get into it.

The Quick Answer

Since you’re likely here for the code, here’s the one-liner. It keeps the first 20 characters and adds an ellipsis:

<?php
$title = "This is a long title for a story";

echo mb_strimwidth($title, 0, 21, "…");
// This is a long title…

mb_strimwidth() counts the … as part of the width, so 21 gives you 20 characters plus the ellipsis. It also leaves short strings alone, so you don’t get an ellipsis on text that already fits.

Using substr()

The classic way is substr(). The 0 is where to start and 20 is how many characters to keep:

<?php
$title = "This is a long title for a story";

echo substr($title, 0, 20) . "…";
// This is a long title…

This works fine for plain English text. Two things to watch for:

  • It always adds the …, even when the text was already short. Check the length first with strlen().
  • It counts bytes, not characters. Accented letters and emoji take more than one byte, so substr() can cut one in half and leave a broken character.

One thing worth knowing: mb_substr() and mb_strlen() count real characters, so they avoid the second problem:

<?php
$dessert = "Crème brûlée";

echo strlen($dessert);          // 15 (bytes)
echo mb_strlen($dessert);       // 12 (characters)

echo substr($dessert, 0, 3);    // "Cr" plus half of "è" (broken)
echo mb_substr($dessert, 0, 3); // Crè

Limit Text Without Cutting Words

Cutting at an exact character count often leaves half a word, like “This is a long title f…”. This function cuts at the last full word that fits instead:

<?php
function limitText(string $text, int $limit, string $end = "…"): string
{
    if (mb_strlen($text) <= $limit) {
        return $text;
    }

    // Grab one extra character so a word ending right at the limit is kept
    $cut = mb_substr($text, 0, $limit + 1);
    $space = mb_strrpos($cut, " ");

    if ($space !== false) {
        $cut = mb_substr($cut, 0, $space);
    } else {
        $cut = mb_substr($text, 0, $limit);
    }

    return rtrim($cut, " ,.;:-") . $end;
}

echo limitText("This is a long title for a story", 22);
// This is a long title…

echo limitText("Short title", 22);
// Short title

If there are no spaces at all (one long word or a URL), it falls back to a hard cut so you still get something back.

In Laravel

If you’re in Laravel, it’s built in:

use Illuminate\Support\Str;

echo Str::limit("This is a long title for a story", 20);
// This is a long title...

Is There a Max String Length in PHP?

Not really. PHP doesn’t cap how long a string can be. On 64-bit PHP, the real limit is your memory_limit setting (128M by default), so a single string can’t be bigger than the memory PHP is allowed to use.

If you’re checking whether text fits a database column, use mb_strlen(). Most MySQL VARCHAR columns count characters, not bytes.

Which One Should You Use?

  • Quick excerpt: mb_strimwidth().
  • Clean previews and meta descriptions: the limitText() function above.
  • Laravel: Str::limit().

I’d skip plain substr() for anything people type in, since names and titles with accents are more common than you’d think.

If you work with strings a lot, here’s how to check if a string is a palindrome in PHP too.

    Slap a sticker on this post

    Comments

    Loading comments…