Laravel Remove All Spaces from String Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Select Specific Columns in Laravel Eloquent Model?
- Laravel Cookies - Get, Set, Delete Cookie Example
- Laravel Ajax CRUD with Popup Modal Example
- How to Run Laravel Project on Different Port?
- Laravel Collection Get First and Last Item Example
- Laravel Amazon S3 File Upload Tutorial
- How to Force Redirect HTTP to HTTPS in Laravel?
- Laravel Custom Forgot & Reset Password Example
- Laravel Razorpay Payment Gateway Example
- Laravel Carbon createFromFormat() Example
- Laravel Google Chart Example Tutorial
- Laravel Livewire Datatables Example Tutorial
- Laravel Redirect Back to Previous Page After Login Example