$string = 'The lazy fox jumped over the fence';
if (str_contains($string, 'lazy')) {
echo "The string 'lazy' was found in the string
";
}
<?php
$string = 'The lazy fox jumped over the fence';
if (str_contains($string, '')) {
echo "Checking the existence of an empty string will always return true";
}
if (str_contains($string, 'lazy')) {
echo "The string 'lazy' was found in the string
";
}
if (str_contains($string, 'Lazy')) {
echo 'The string "Lazy" was found in the string';
} else {
echo '"Lazy" was not found because the case does not match';
}
# Checking the existence of the empty string will always return true
# The string 'lazy' was found in the string
# "Lazy" was not found because the case does not match
$str = 'Hello World!';
if (strpos($str, 'World') !== false) {
echo 'true';
}
if (str_contains('How are you', 'are')) {
echo 'true';
}
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
if (strpos($a, 'are') !== false) {
echo 'true';
}
str_contains(string $haystack , string $needle);
string = 'The lazy fox jumped over the fence';
if (str_contains($string, 'lazy')) {
echo "The string 'lazy' was found in the string
";
}
// For php < 8
/**
* Determine if a string contains a given substring.
*
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_contains($haystack, $needle)
{
return $needle !== '' && self::strpos($haystack, $needle) !== false;
}