Laravel 5 String Helpers: Replacing the First Occurrence of a Value

Laravel 5

The replaceFirst helper method is used to replace the first occurrence of a given $search string with the provided $replace string in the $subject string. All three parameters are required and must be strings.


The signature of the replaceMethod is:

public static function replaceFirst(

Example Use

The following examples highlight the basic usage of the replaceFirst method. The results of the method call will appear above the call as a comment.

use Illuminate\Support\Str;

// //
    'http://', '//', 
    '' .

// Hello, there!
Str::replaceFirst('there', 'Hello', 'there, there!');

Global str_replace_first Helper Function

The str_replace_first function is a shortcut to calling Str::replaceFirst. This function is declared in the global namespace.

Start the Discussion

Leave a comment

Subscribe to our newsletter