Laravel Remove Quotes/Any Character from String

avatar
Dec 20, 2022 · Article · 2 min, 505 words

Hello Artisans, today I'll show you how to remove quotes from string in your laravel application. From todays tutorial we cannot only remove quotes from string but also can remove any characters we want. So, let's get started and see how we can easily remove quotes/ special character from any string in our laravel application.  

Note: Tested on Laravel 9.19

Table of Contents

  1. Create and Setup Controller
  2. Create Route
  3. Output

Create and Setup Controller

First of all, create a controller so that we can remove quotes (or any string we want) from the string . So, fire the below commands in the terminal.

php artisan make:controller HomeController

It'll create a controller under app\Http\Controllers called HomeController.php. Open the file and replace with below codes.

app/Http/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function removeWords()
    {
        $str = 'Welcome to "shouts.dev"';
        dd(str_replace('"', '', $str));
    }
}

Create Route

Now we'll create routes so that we can see the output of our string after removing the quotes. So, put the below route in web.php file. 

routes/web.php
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('remove/words', [\App\Http\Controllers\HomeController::class,'removeWords']);

Output

And finally we're ready with our setup. It's time to check our output. Now go to http://127.0.0.1:8000, If everything goes well (hope so) you'll find a below output

String after removing Quotes

That's it for today. I hope you've enjoyed this tutorial. You can also download this tutorial from GitHub. Thanks for reading. 🙂

Comments

No comments yet…