Laravel Limit Characters - PHP
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Limiting Character Count in Laravel with PHP
Introduction: Welcome to our technical blog that delves into the world of Laravel, one of the most popular PHP frameworks for building powerful web applications. Today, we will discuss a common issue faced by developers while displaying post titles on their applications. Specifically, how can you limit the title name to show only the first 10 characters? Let's explore this topic in depth with relevant code examples and best practices.
Step 1: Understanding Laravel Blade Templates
Laravel utilizes the Blade template engine for rendering dynamic web pages. Using the code you provided, `{{ $post->nextPost()['title'] }}`, you can access the next post's title from a specific collection. However, this may show full titles that are longer than necessary.
Step 2: Implementing String Manipulation Techniques in PHP
To limit the character count of a string in Laravel's Blade templates, we need to use PHP's built-in functions for string manipulation. A few popular functions include `substr()`, `strlen()`, and `strslice()`. Here are some examples:
Example 1 - Using `substr()`:
```php
$title = "The Quick Brown Fox Jumps Over the Lazy Dog";
echo substr($title, 0, 10); // Outputs "The Quic"
```
In this example, we used `substr()` to extract the first 10 characters from the given string. If the title is longer than 10 characters, this will automatically truncate it.
Example 2 - Using `strlen()`:
```php
$title = "The Quick Brown Fox Jumps Over the Lazy Dog";
if (strlen($title) > 10) {
$shortenedTitle = substr($title, 0, 10);
} else {
$shortenedTitle = $title;
}
echo $shortenedTitle; // Outputs the shortened title if it is longer than 10 characters, otherwise displays the entire title.
```
Here, we used `strlen()` to check the length of our string. If it exceeds 10 characters, we use `substr()` as in Example 1. Otherwise, we display the full title without any manipulation.
Example 3 - Using `strslice()`:
```php
$title = "The Quick Brown Fox Jumps Over the Lazy Dog";
echo strslice($title, 0, 10); // Outputs "The Quic"
```
In this example, we used Laravel's `strslice()`, a wrapper for PHP's `substr()` function. It provides a more concise syntax to perform the same task as Example 1.
Step 3: Integrating Code with Blade Templates
Now that you understand how to manipulate strings in your code, let's incorporate this functionality into your Laravel application's Blade templates. Here is an updated version of your initial example:
```php
{{ strslice($post->nextPost()['title'], 0, 10) }}
```
This code ensures that next post titles rendered using the `span` tag will only display up to 10 characters. For any longer title, it will automatically truncate and show the first 10 characters.
Conclusion:
By following these steps, you can efficiently limit character counts in your Laravel application's Blade templates while ensuring cleaner and more readable output. Remember that the best way to solve a problem is by choosing the right tools for the job. In this case, Laravel provides useful functions like `substr()`, `strlen()`, and `strslice()` to help you achieve your goal. Feel free to explore other Laravel topics on our blog at https://laravelcompany.com!