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.

Signature

The signature of the replaceMethod is:

{language=php}

public static function replaceFirst(
        $search,
        $replace,
        $subject
    );

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;

// //maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css
Str::replaceFirst(
    'http://', '//', 
    'http://maxcdn.bootstrapcdn.com/bootstrap/' .
          '3.3.6/css/bootstrap.min.css'
);

// 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.