How to Replace String in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Developer,

This extensive guide will teach you how to replace string in laravel. I would like to share with you laravel string replace all. We will use laravel string replace example. If you have a question about laravel str_replace then I will give a simple example with a solution.

We will use Str facade to replace string in laravel. i will give you few examples to replace string in laravel. so, let's see the simple example:

Laravel Replace String in Controller

you can use it with controller like this way:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$string = 'He is Paresh';

$replaced = Str::replace('Paresh', 'Hardik', $string);

dd($replaced);

}

}

Output:

He is Hardik

Laravel Replace String in Blade File

you can use it with blade file like this way:

Blade File Code:

<p>{{ Str::replace('Paresh', 'Hardik', 'He is Paresh') }}</p>

Output:

He is Hardik

I hope it can help you...

Tags :
Shares