Looking to hire Laravel developers? Try LaraJobs

laravel-stringable-extras maintained by sumaiazaman

Description
Missing whenDoesntContain and whenDoesntContainAll methods for Laravel's Stringable class.
Author
Last update
2026/05/22 11:54 (dev-main)
License
Links
Downloads
0

Comments
comments powered by Disqus

Laravel Stringable Extras

Latest Version on Packagist Tests License

Adds the missing whenDoesntContain() and whenDoesntContainAll() methods to Laravel's fluent Stringable class.

Laravel ships with whenContains() and whenContainsAll(), and has negative counterparts for start/end (whenDoesntStartWith, whenDoesntEndWith), but the negative counterpart for contains was never added to the framework. This package fills that gap.

Requirements

  • PHP 8.2+
  • Laravel 11, 12, or 13

Installation

composer require sumaiazaman/laravel-stringable-extras

The service provider is auto-discovered — no manual registration needed.

Usage

whenDoesntContain()

Executes the callback when the string does not contain the given substring. Accepts a string or an array of strings (any match counts).

// Callback fires — string doesn't contain 'xxx'
$result = str('hello world')
    ->whenDoesntContain('xxx', fn ($s) => $s->upper());
// 'HELLO WORLD'

// Callback is skipped — string contains 'world'
$result = str('hello world')
    ->whenDoesntContain('world', fn ($s) => $s->upper());
// 'hello world'

// With a default callback
$result = str('hello world')
    ->whenDoesntContain(
        'world',
        fn ($s) => $s->upper(),
        fn ($s) => $s->title(),
    );
// 'Hello World'

// Array of needles — callback fires only if none are present
$result = str('hello world')
    ->whenDoesntContain(['foo', 'bar'], fn ($s) => $s->upper());
// 'HELLO WORLD'

whenDoesntContainAll()

Executes the callback when the string does not contain all of the given substrings. If even one needle is missing, the callback fires.

// Callback fires — 'xxx' is not in the string
$result = str('hello world')
    ->whenDoesntContainAll(['hello', 'xxx'], fn ($s) => $s->upper());
// 'HELLO WORLD'

// Callback is skipped — both needles are present
$result = str('hello world')
    ->whenDoesntContainAll(['hello', 'world'], fn ($s) => $s->upper());
// 'hello world'

Testing

composer test

License

MIT. See LICENSE.