Laravel String Helper Function: str_replace_first

November 16, 2016 —John Koster

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 for the replaceFirst helper method is:

replaceFirst($search, $replace, $subject)

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.

1use Illuminate\Support\Str;
3// //
5 'http://', '//',
6 ''
9// Hello, there!
10Str::replaceFirst('there', 'Hello', 'there, there!');

#str_replace_first($search, $replace, $subject)

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

Some absolutely amazing

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.