Skip to content

[5.8] adds Str::containsAll() method - #28806

Merged
taylorotwell merged 2 commits into
laravel:5.8from
chrisbbreuer:feature/add-str-contains-all
Jun 12, 2019
Merged

taylorotwell merged 2 commits into
laravel:5.8from
chrisbbreuer:feature/add-str-contains-all

Conversation

@chrisbbreuer

@chrisbbreuer chrisbbreuer commented Jun 11, 2019 •

Copy link
Copy Markdown
Contributor

Hi everyone,

As discussed here #28688 and #28695 - in order to avoid a breaking change, I abstracted it into a new method called containsAll() and I am using contains() to check whether the value is contained inside the string.

Every now and then I run into a dilemma where I want to check if an array of values is contained within a string.

This adds the functionality to check for all array values inside a string.

Check out the tests to see all the supported scenarios.

I hope you guys find this useful, too 🙂

Chris

Comment thread src/Illuminate/Support/Str.php Outdated
* @param string|array $needles
* @return bool
*/
public static function containsAll($haystack, $needles)

@timacdonald timacdonald Jun 12, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd would think this could explicitly require an array for $needles. Otherwise you would just use Str::contains() for a single item.

i.e. I'm not sure Str::containsAll($names, 'Tim'); makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, Tim. I have asked myself the same question on whether a string should or should not be allowed. I thought it wouldn't "hurt" to keep the functionality and possibly could come in handy at times.

But you're right; logically, Str::contains() would be a more suitable call for that.

Definitely open for a discussion here on whether to keep or to remove it!

@taylorotwell
taylorotwell merged commit a986126 into laravel:5.8 Jun 12, 2019
salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
…ins-all

[5.8] adds Str::containsAll() method
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.

3 participants