Checking if a String Starts With a Particular Value in Laravel - Laravel 5.5 String Helper Method startsWith

1 min read

startsWith does the opposite of ends_with, and has the same rules of use. Instead of checking if a given $haystack ends with any of the supplied $needles, startsWith checks if a given $haystack starts with any of the $needles.

Signature

The signature of the startsWith method is:

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

Example Use

1use Illuminate\Support\Str;
2 
3// true
4Str::startsWith('A simple sentence.', 'A');
5 
6// false
7Str::startsWith('No punctuation here', '.');
8 
9// false
10Str::startsWith('Case matters', 'c');
11 
12// true
13Str::startsWith('CASE STILL MATTERS', 'C');

Global starts_with Helper Function

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

Thanks for taking the time to read this post! If you found this article useful and want to help support more work like this, please consider sponsoring my work on GitHub, or by checking out some merch.

 Sponsor on GitHub  Shop Merch