How to get the last 4 characters in a string - Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Extract the Last Characters from a String in Laravel: A Developer's Guide
When working with data retrieved from a database, often the raw string requires some manipulation before it is displayed to the end-user. In your specific scenarioâextracting the last four characters or parts of a string like `12-12-2067` to get just `2067`âyou need robust string handling techniques in PHP, which Laravel heavily leverages.
As a senior developer, I believe there are several ways to approach this, depending on whether you want the logic handled in the database (most efficient) or within your application layer (most flexible). Letâs explore the best methods for achieving this clean data extraction within a Laravel context.
## The Core Challenge: String Manipulation
Your goal is to isolate the final segment of a string. Since standard SQL functions can handle this, we often look for database-level solutions first. However, if the manipulation is complex or specific to application logic, PHP's built-in string functions are the tool of choice.
Letâs assume your retrieved string is stored in a variable named `$fullString`.
```php
$fullString = '12-12-2067';
// We want '2067'
```
## Method 1: Using PHP String Functions (Application Layer)
The most straightforward way to handle this in Laravel is by manipulating the string once you have fetched it. For extracting characters from the end of a string, the `substr()` function is perfect. To get the last four characters, you use negative indexing.
```php
$fullString = '12-12-2067';
// Extracting the last 4 characters
$lastFourChars = substr($fullString, -4);
// Output: "2067"
```
**Explanation:**
The `substr(string $string, int $start, ?int $length = null)` function extracts a part of a string. When you use a negative value for the `$start` parameter (like `-4`), PHP counts backward from the end of the string, effectively selecting the final four characters. This is highly efficient for simple suffix extraction.
## Method 2: Database-Level Extraction (The Efficient Approach)
While application-layer processing works fine, pushing data transformation down to the database is often more performant, especially when dealing with very large datasets. If your database supports string functions (like MySQL's `RIGHT()` or PostgreSQL's `SUBSTRING()`), you can let the database handle the extraction before sending the data over the network.
If your goal is specifically to get the part after the last delimiter (like the last segment separated by a hyphen), you would use functions like `SUBSTRING_INDEX` in MySQL:
```sql
SELECT SUBSTRING_INDEX(your_column, '-', -1) AS last_part
FROM items;
```
This SQL query finds the substring starting from the last occurrence of the delimiter (`-`) and goes to the end of the string. This delegates the heavy lifting to the database engine, which is usually optimized for this kind of operation.
## Method 3: Handling Complex Splitting in Laravel Models
If your data structure involves multiple delimiters (e.g., you want the last segment when a string has several hyphens), you might need to split the string into an array first. This pattern is very common when dealing with semi-structured data within Eloquent models.
In your Model or Controller, before passing it to the view, you can use PHP's `explode()` function:
```php
$fullString = '12-12-2067';
$parts = explode('-', $fullString); // $parts will be ['12', '12', '2067']
// Get the last element of the array
$lastSegment = end($parts);
// Output: "2067"
```
This approach is excellent for ensuring data integrity, as it separates the parsing logic from the presentation layer. For complex data interactions within Laravel, always remember that understanding how Eloquent interacts with the underlying database structure is key to writing clean, high-performing code, much like the principles discussed at [laravelcompany.com](https://laravelcompany.com).
## Conclusion
For extracting the last four characters from a string in a Laravel application, the best approach depends on context:
1. **For simple character extraction:** Use `substr($string, -4)` in your controller or view logic.
2. **For complex segment parsing (like delimited data):** Use `explode()` and `end()` to split the string into an array and retrieve the final element.
3. **For maximum performance on large datasets:** Explore native SQL functions like `SUBSTRING_INDEX` to let the database handle the manipulation.
By choosing the right tool for the job, you ensure your Laravel application remains clean, efficient, and highly maintainable.