Add ... if string is too long PHP

Add ... if string is too long PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Adding Ellipses to Long Strings in PHP

Handling text truncation is a common requirement when building user interfaces, especially when dealing with database fields like descriptions that vary wildly in length. You might display the full content on an admin dashboard but need a condensed preview on a public listing page. The goal is straightforward: truncate strings exceeding a specific character count and append an ellipsis (...) only when necessary, preserving the integrity of shorter strings.

In PHP, this logic can be implemented cleanly using the substr() function combined with a conditional check. The core challenge lies in distinguishing between a string that naturally fits within the limit and one that requires truncation. If you blindly apply substring operations without checking the length first, you risk altering short strings or creating inconsistent UI behavior where some items show ellipses while others do not.

The Basic Implementation

The most reliable approach involves calculating the length of the input string before attempting to slice it. You can use the strlen() function to determine the total number of characters. If the length is greater than your threshold (e.g., 50), you apply substr() to extract the first portion and concatenate the ellipsis.

Here is a practical implementation:

function truncateDescription($string, $limit = 50) {
    if (strlen($string) > $limit) {
        return substr($string, 0, $limit) . '...';
    }
    return $string;
}

This pattern ensures that strings under or equal to the limit are returned exactly as stored in the database. For instance, a string with exactly 50 characters remains untouched, avoiding the visual confusion of an ellipsis on a complete thought. Conversely, a string longer than 50 characters gets sliced at index 49 (since substr is zero-indexed) and appended with the indicator.

Handling HTML Entities

A subtle pitfall arises when your description field contains HTML entities or special characters that strlen() might not count as you expect depending on how the data is stored. If your database stores raw HTML like  , strlen() counts it as three characters, whereas displaying it should render as a single space.

If truncation must occur at visual character boundaries rather than byte or entity boundaries, simple substr() operations can break tags in half. For example, slicing a string containing <a href="long-url"> might result in malformed HTML if the cut happens mid-tag. In such cases, you would need to strip HTML tags before counting characters or use a library that understands DOM structures to count visible nodes. However, for plain text descriptions, the strlen() approach is efficient and sufficient.

Database-Level Truncation vs. Application Logic

While it is tempting to handle this logic directly in the SQL query using functions like SUBSTRING_INDEX or LEFT, keeping the truncation logic in the application layer (PHP) offers more flexibility. Database-side truncation requires repeating the conditional logic for every page that needs different display rules, such as showing full text on one page and truncated text on another.

By calculating the length in PHP, you can easily toggle between displaying the full field or the truncated version based on the context of the current view. This separation of concerns keeps your database queries clean and allows you to adjust truncation limits dynamically without altering schema or SQL logic.

For projects built with Laravel, you might find utility libraries or packages that simplify string manipulation further, such as those available at https://laravelcompany.com, but the native PHP functions remain robust for standard use cases.

Our network