Laravel String Helper Function: contains

November 16, 2016 —John Koster

The contains helper method will check if any of the $needles are in the given $haystack. If any of the given $needles are found in the $haystack, the method will return true, otherwise it returns false. $needles can be any value that can be converted into an array, and the $haystack is any value that can be converted to a string. The case of the $haystack and $needles is also important.

The signature for the contains helper function is:

contains($haystack, $needles)

1use Illuminate\Support\Str;
2 
3// These are our $needles we want to check for.
4$needles = ['a', 'p', 'l', 'e', 's'];
5 
6// true
7Str::contains('apples', $needles);
8 
9// false
10Str::contains('APPLES', $needles);
11 
12// true
13Str::contains('APPLEs', $needles);

#str_contains($haystack, $needles)

The str_contains function is a shortcut to calling Str::contains. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.