Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

honzahana's avatar

Extend Str helper

Is there a way to extend Larave's build in \Illuminate\Support\Str helper? I would like to add new functionalities.

If i create a App\Helpers\Str class which extend original \Illuminate\Support\Str

namespace App\Helpers;

class Str extends \Illuminate\Support\Str {
    public static function removeSpaces(string $value): string
    {
        return preg_replace('/[\s]+/u', '', $value);
    }
}

Fluet access does not work.

Example: Str::of('ahoj, jak se mas?')->removeSpaces()->title()

Do you have any idea?

0 likes
12 replies
bobbybouwmann's avatar

The Str class is Macroable. This means you can actually add methods to it on runtime. You can do it like so

use Illuminate\Support\Str;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Str::macro('removeSpaces', function (string $value) {
            return preg_replace('/[\s]+/u', '', $value);
        });
    }
}

Let me know if that works for you ;)

honzahana's avatar

Hi Bobby, this solution does not work.

class AppServiceProvider extends ServiceProvider
{
    /**
    * Register any application services.
    *
    * @return void
    */
    public function register()
    {
        //
    }

    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
        \Illuminate\Support\Str::macro('removeSpaces', function (string $value) {
            return preg_replace('/[\s]+/u', '', $value);
        });
    }
}

If I try Str::removeSpaces("aho a") that works great, but fluent access does not:

Str::of("aho a")->removeSpaces()

Exists with

BadMethodCallException with message 'Method Illuminate/Support/Stringable::removeSpaces does not exist.'

I also prefer not to register this with Providers. This causes the code is executed each time and performance goes down. A better solution would be on demand (like use class).

Thank you!

bobbybouwmann's avatar

Yes! @bryank is correct.

If you want this method available using the Str::of($string) call, you need to add the macroable in there. Something like this

use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Str::macro('removeSpaces', function (string $value) {
            return preg_replace('/[\s]+/u', '', $value);
        });

        Stringable::macro('removeSpaces', function (string $value) {
            return new Stringable(preg_replace('/[\s]+/u', '', $value));
        });
    }
}

Note: You need to return a Stringable instance here, otherwise fluent access won't work

1 like
honzahana's avatar
honzahana
OP
Best Answer
Level 7

It still does not work.

Stringable ends with error:

TypeError: Too few arguments to function Illuminate/Support/Stringable::App/Providers/{closure}(), 0 passed and exactly 1 expected

It looks like the correct version is (file app/Providers/AppServiceProvider.php):

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use \Illuminate\Support\Str;
use \Illuminate\Support\Stringable;

class AppServiceProvider extends ServiceProvider
{
	/**
	* Register any application services.
	*
	* @return void
	*/
	public function register()
	{
		//
	}

	/**
	* Bootstrap any application services.
	*
	* @return void
	*/
	public function boot()
	{
		Str::macro('removeSpaces', function (string $value) {
			return preg_replace('/[\s]+/u', '', $value);
		});

		Stringable::macro('removeSpaces', function () {
			return new Stringable(preg_replace('/[\s]+/u', '', $this->value));
		});
	}
}
1 like
MichalOravec's avatar

@honzahana It should be like this

Stringable::macro('removeSpaces', function ($value) {
    return new Stringable(preg_replace('/[\s]+/u', '', $value));
});
MichalOravec's avatar

@honzahana I don't have to test it.

You had this error

TypeError: Too few arguments to function Illuminate/Support/Stringable::App/Providers/{closure}(), 0 passed and exactly 1 expected

It will fix it with this

Stringable::macro('removeSpaces', function ($value) {
    return new Stringable(preg_replace('/[\s]+/u', '', $value));
});

If it don't work, I mean removeSpaces I don't know, but that error was because you don't copy whole code from @bobbybouwmann

honzahana's avatar

No, I tested it and it doesn't work.

You use fluent string this way: Str::of('393 01')->removeSpaces() so you not provide any argument to the Closure. This is why it ends with an error.

Open \Illuminate\Support\Stringable and will see this:

public function camel()
{
    return new static(Str::camel($this->value));
}

Laravel also doesn't use any parameters.

MichalOravec's avatar

@honzahana Try it like this

Stringable::macro('removeSpaces', function () {
    return (string) new static(preg_replace('/[\s]+/u', '', $this->value));
});
honzahana's avatar

Code I wrote here works well.

@michaloravec There is a bug in your code. You can't convert Stringable to string, because of this: Str::of("393 01")->removeSpaces()->title()

PHP Error:  Call to a member function title() on string in Psy Shell code on line 1
bobbybouwmann's avatar

@honzahana This works

use Illuminate\Support\Stringable;

Stringable::macro('removeSpaces', function () {
    return new Stringable(preg_replace('/[\s]+/u', '', $this->value));
});

(string) Str::of(' test ')->removeSpaces();
1 like

Please or to participate in this conversation.