limit text length in php and provide 'Read more' link

limit text length in php and provide 'Read more' link

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

Stefan Bogdanescu

Founder & Senior Architect

Limiting Text Length in PHP and Implementing a 'Read More' Link

Handling dynamic content length is a common requirement when rendering articles, blog posts, or news feeds. If you are storing text in a PHP variable like $text, where that value might range from a concise summary to a sprawling 10,000-word essay, your layout will likely break without intervention. A page that expands infinitely based on content length disrupts the reading experience and breaks responsive design grids.

The goal is usually straightforward: truncate the display to a fixed character count (e.g., 500 characters) and append a "Read more" link. Clicking this link should reveal the full text, often via a modal or by expanding the container. While simple string manipulation seems trivial, choosing the right approach depends heavily on how the data is stored and retrieved.

Truncation Strategies: Characters vs. Words

The most naive implementation involves using substr() to slice the first 500 characters of $text. This works fine for arbitrary strings, but it fails catastrophically if your text contains HTML tags. If you blindly cut a string in the middle of a <p> tag or inside a CSS class attribute, you will break the DOM structure and render broken elements on the client side.

Before applying substr(), you must ensure the string is plain text or that you are handling HTML safely. If $text contains raw HTML, simply cutting characters is dangerous. You need to strip tags first or find a way to cut at a safe boundary. A safer approach for HTML content is to use DOMDocument or striptags() before truncation, though this adds overhead.

If your text is stored as plain text in the database and you only add HTML formatting on the fly within the view, character-based truncation is acceptable, provided you wrap the output correctly. However, if you prefer word boundaries rather than character limits, wordwrap() can help, but it does not support HTML tags directly without additional processing to preserve them.

Implementing the Truncation Logic

Assuming $text is a plain string or has been sanitized of dangerous HTML, here is a robust pattern for truncating to 500 characters:

$limit = 500;
$ellipsis = '...';

if (strlen($text) > $limit) {
    // Truncate and append ellipsis
    $displayText = substr($text, 0, $limit) . $ellipsis;
} else {
    $displayText = $text;
}

// Wrap in a link that reveals the full text
$fullReadMore = <<<HTML
<div class="excerpt">{$displayText}</div>
<a href="#full-text-modal" class="read-more-btn">Read more</a>
<script>
document.querySelector('.read-more-btn').addEventListener('click', function() {
    document.getElementById('full-text-modal').style.display = 'block';
});
</script>
<div id="full-text-modal" style="display:none;">{$text}</div>
HTML;

This approach manually manages the truncation logic. You can also use mb_substr() if your text contains multi-byte characters (like emojis or non-ASCII languages) to ensure accurate character counting according to Unicode standards. Standard strlen() counts bytes, which can lead to incorrect truncation in UTF-8 strings containing complex characters.

Security and State Management

When using a modal to display the full text, you must be vigilant about security. If $text comes from a database, ensure it is escaped before echoing or stored with proper sanitization. Storing HTML directly in a variable for later insertion into a DOM element requires careful handling; otherwise, you risk Cross-Site Scripting (XSS) vulnerabilities if the content was not strictly validated on input.

Using a modal via JavaScript to show the full text avoids reloading the page, which is generally preferred for single-page applications or modern UX standards. However, this introduces state management considerations. The modal must be hidden initially and only displayed upon interaction. Ensure that the $text variable passed into the view remains consistent between the truncated preview and the full modal content.

If you are using a framework like Laravel, consider utilizing Blade directives or helpers to encapsulate this logic rather than repeating string operations in every view. While simple PHP scripts suffice for static pages, frameworks offer better separation of concerns when dealing with dynamic content streams.

Final Considerations on User Experience

The "Read more" pattern is effective only if the preview provides enough context. Truncating too aggressively (e.g., 50 characters) might leave users guessing about the topic, rendering the link less valuable. Conversely, truncating too much defeats the purpose of the summary. A sweet spot often lies between 300 and 600 characters, depending on your typography and reading level assumptions.

Always test how different content lengths affect your layout. Long titles or specific phrasing can push the truncated text beyond your intended break point. If you find that the ellipsis appears awkwardly placed, you may need to adjust the limit dynamically based on paragraph boundaries rather than a strict character count.

Our network