Search⌘ K
AI Features

Replacing the Last Occurrence of a Substring or Character

Explore how to use Laravel's replaceLast helper method to find and replace the last occurrence of substrings in strings. Understand practical uses like changing file extensions, formatting currencies, and modifying namespaces. Apply this method with collections for batch processing and enhance your Laravel string manipulation skills.

We'll cover the following...

The replaceLast helper method

The replaceLast helper method is the opposite of replaceFirst. We can use it to find the last occurrence of some search string and replace it with another value.

PHP
<?php
/**
* Versions: Laravel 8, 9
*
* @return string
*/
public static function replaceLast(
string $search,
string $replace,
string $subject
);
...