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()
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...
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?