Skip to content

Conversation

@ksassnowski
Copy link
Contributor

This PR adds a third parameter $ignoreCase to Str::contains and Str::containsAll to allow for case-insensitive comparisons.

Motivation

I often find myself doing something like this in my code

Str::contains(Str::lower($user->first_name), Str::lower($request->query('q'));

This gets especially annoying when trying to compare against multiple fields

$normalizedQuery = Str::lower($request->query('q'));

$matches = Str::contains(Str::lower($user->first_name), $normalizedQuery) 
          || Str::contains(Str::lower($user->last_name), $normalizedQuery);

Changes

With this PR, it would be possible to do this instead:

Str::contains($user->first_name, $request->query('q'), true);

I added the same parameter to Str::containsAll. To avoid doing the transformation inside a loop, it gets applied only once inside containsAll before calling static::contains.

The $ignoreCase parameter is false by default so the existing behavior doesn’t change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants