Join WhatsApp ChannelJoin Now

Laravel 10 Remove String All Spaces Example

Hi dev,

Today, i show you laravel remove string all spaces example. This article will give you simple example of laravel remove string all spaces example. you will laravel remove string all spaces example. In this article, we will implement a laravel remove string all spaces example.

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.

So, let’s follow few steps to create example of laravel remove string all spaces example.

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

Output:

Hi,Thisisfromcodeplaners.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 codeplaners.com";
  
        $string = preg_replace('/\s+/', '', $string);
 
        dd($string);
    }
}

Output:

Hi,Thisisfromcodeplaners.com

I hope it will assist you…

Recommended Posts