Laravel Remove All Whitespace From String

May 20, 2023 | Category : Other

Hey,Hello Folks,
If you need to see an example of laravel remove all spaces from string. you can see laravel remove all whitespace from string. step by step explain how to remove spaces from string in laravel. you can understand a concept of how to remove space from string in laravel.

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 and laravel 10 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 HDTuto.com";

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

dd($string);

}

}

Output:

Hi,ThisisfromHDTuto.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 HDTuto.com";

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

dd($string);

}

}

Output:

Hi,ThisisfromHDTuto.com

I hope it can help you...