How to use is() Method to Match One or An Array of Strings in Laravel?

Laravel Str Support library is a very useful resource when it comes to String methods. Lets check the is() method from it. It takes 2 parameters. First one could be an array or just a string and the second one has to be a string. It will match taking one string or a series of strings in the whole array given in the first parameter and match that to the string provided in second array. Let’s write the code and see how it goes.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Str;


class HomeController extends Controller {
	/**
	 * Show the application dashboard.
	 *
	 * @return \Illuminate\Contracts\Support\Renderable
	 */
	public function index() {
		$found = Str::is( [ 'story', 'pen', 'film', 'tv' ], 'pen' );
		if ( $found ) {
			echo "String has been found in the array.";
		} else {
			echo "String has not been found in the array.";
		}
	}
}

Now is() method will match ‘pen’ string from every element of the array provided as first parameter and return the result. In the case mentioned in code above, string will be matched. This method definitely saved for each loop complexity.

For more detailed documentation check Laravel 8.0 docs. Let’s know in the comments if you face any difficulty. We will address that.