Skip to content

[9.x] Add option to ignore case in Str::contains and Str::containsAll #37330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2021
Merged

[9.x] Add option to ignore case in Str::contains and Str::containsAll #37330

merged 1 commit into from
May 10, 2021

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