In this tutorial i have to 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.
Example 1: using str_replace()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestingController extends Controller
{
public function index()
{
$string = "Hi, This Amit Software Developer at Cotocus PVT LTD";
$string = str_replace(' ', '', $string);
return response($string);
}
}
Output: -
Example 2: using preg_replace()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestingController extends Controller
{
public function index()
{
$string = "Hi, This Amit Software Developer at Cotocus PVT LTD";
$string = preg_replace('/\s+/', '', $string);
return response($string);
}
}
Output:-