Laravel Remove Space from String Example

Sep 15, 2022 . Admin



Hello Friends,

Here, I will show you laravel remove space from string example. We will use how to remove space from a string in laravel. we will help you to give example of remove spaces from a string in output with laravel. This post will give you simple example of how to remove white space from the end of a string in laravel. So, let's follow few step to create example of laravel remove all whitespace code example.

The bellow example will only remove spaces.You can simply use the PHP str_replace() function to strip or remove all spaces inside a string. you can pass PHP command to laravel controller and use that function.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Step 2: Create RemoveSpaceController

we will create RemoveSpaceController with removeSpace() method to remove space from string. so you can see the below code with output:

php artisan make:controller RemoveSpaceController
App\Http\Controllers\RemoveSpaceController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RemoveSpaceController extends Controller
{
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $str = 'This is a simple piece of text.';
        dd($this->removeSpace($str));
    }

    /**
     * Display a listing of the resource.
     *
     * remove space function
     */
    public function removeSpace($var)
    {
        return str_replace(' ', '', $var);
    }
}

Step 3: Create Format Route web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RemoveSpaceController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('remove',[RemoveSpaceController::class,'index']);
Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/remove
Output:
Thisisasimplepieceoftext.

It will help you...

#Laravel