Search

Laravel 5.5 String Helper Method: contains

November 30, 2017 —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.

#Signature

The signature of the contains method is:

1public static function contains(
2 $haystack,
3 $needles
4 );

#Example Use

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);

#Global str_contains Helper Function

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.