PHP function to Determine if string contains given substring str_contains()

Here is simplest PHP function to determine if given string contains given substring or specific words or special characters. 


In this function we have used predefined php function called strpos

By using str_contains function you can also check multiple strings by giving an array to 2nd parameter as needles.

Source Code:

<?php

/**
* Determine if a given string contains a given substring.
*
* @param  string  $haystack
* @param  string|array  $needles
* @return bool
*/
function str_contains($haystack, $needles)
{
	foreach ((array) $needles as $needle)
	{
		if ($needle != '' && strpos($haystack, $needle) !== false) return true;
	}

	return false;
}


Output / Usage:

<?php 

$string = "PHP function to Determine if string contains given substring";

if(str_contains($string,'string')){
 echo 'TRUE';
} else {
echo 'FALSE';
}

Comments: (Your feedback is valuable to us)