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



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()
    {
        $beforeLast1 = Str::beforeLast('This is my name', 'name');
        
        print_r($beforeLast1);
        // output - my

        $beforeLast2 = Str::beforeLast('This is a Car','is');
        
        print_r($beforeLast2);
        // output - This
    }
}

May this example help you.