Best Cosmetic Hospitals Near You

Compare top cosmetic hospitals, aesthetic clinics & beauty treatments by city.

Trusted • Verified • Best-in-Class Care

Explore Best Hospitals

Remove All Spaces from String In Laravel

Laravel

I’d like to demonstrate how to remove every space from a string using Laravel today. We’ll look at a Laravel example that eliminates all whitespace from a string. I’ll demonstrate how to eliminate spaces from strings in Laravel. We’ll utilise the Laravel method for removing spaces from strings. So, let’s examine an example in more detail.

Here, I’ll show you how to eliminate every space from a string in a Laravel project using just two methods. Laravel’s str_replace() and preg_replace() functions will be used to eliminate all whitespace from strings.

This example works with versions of Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10.

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 abhishek";
  
        $string = str_replace(' ', '', $string);
 
        dd($string);
    }
}

// Output:
HiThisisabhishek

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:
HiThisisabhishek

Best Cardiac Hospitals Near You

Discover top heart hospitals, cardiology centers & cardiac care services by city.

Advanced Heart Care • Trusted Hospitals • Expert Teams

View Best Hospitals
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x