Laravel Remove All Spaces from String Example

By Hardik Savani April 16, 2024 Category : Laravel

Hey,

Today, I would like to show you laravel remove all spaces from string. We will look at an example of laravel remove all whitespace from string. I’m going to show you about how to remove spaces from string in laravel. We will use how to remove space from string in laravel. So, let us see in detail an example.

Here, i will give you simply two ways to remove all spaces from string in laravel project. we will use str_replace() and preg_replace() functions to remove all whitespace from string in laravel.

you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Example 1: using str_replace()

<?php

namespace App\Http\Controllers;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$string = "Hi, This is from ItSolutionStuff.com";

$string = str_replace(' ', '', $string);

dd($string);

}

}

Output:

Hi,ThisisfromItSolutionStuff.com

Example 2: using preg_replace()

<?php

namespace App\Http\Controllers;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$string = "Hi, This is from ItSolutionStuff.com";

$string = preg_replace('/\s+/', '', $string);

dd($string);

}

}

Output:

Hi,ThisisfromItSolutionStuff.com

I hope it can help you...

Tags :
Shares