Hello Devs, 

In this tutorial, we will learn Laravel str replaceFirst() function Example

The Str::replaceFirst method replaces the first occurrence of a given value in a string.

Follow this step-by-step guide below. 


Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\FileController;
use Illuminate\Support\Str;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()

    {
        $replaced1= Str::replaceFirst('I', 'We', 'I will show example');
        dd($replaced1);
        // output - We will show example

        $replaced2 = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
        dd($replaced2);
        // a quick brown fox jumps over the lazy dog
    }
}

Output

"We will show example"
"a quick brown fox jumps over the lazy dog"


May this help you.