PHP Version
8+
$haystack = 'Hello! This is codingCourses.';
$needle = 'codingCourses';

if (str_contains($haystack, $needle)) {
    echo "The string contains '$needle'.";
} else {
    echo "The string does not contain '$needle'.";
}

// Output: The string contains 'codingCourses'.
str_contains(string $haystack, string $needle): bool
if (str_contains('abc', '')) {
    echo 'Checking for an empty string always returns true.';
}

// Output: Checking for an empty string always returns true.
$haystack = 'Hello, World!';

if (str_contains($haystack, 'world!')) {
    echo "The string contains 'world!'.";
} else {
    echo "Case does not match, so 'world!' was not found.";
}

// Output: Case does not match, so 'world!' was not found.
if (!function_exists('str_contains')) {
    /*
     * Example polyfill for str_contains()
     * Source: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/compat.php#L423
     */
    function str_contains($haystack, $needle) {
        if ('' === $needle) {
            return true;
        }

        return false !== strpos($haystack, $needle);
    }
}

$haystack = 'Hello! This is codingCourses.';
$needle = 'codingCourses';

if (str_contains($haystack, $needle)) {
    echo "The string contains '$needle'.";
} else {
    echo "The string does not contain '$needle'.";
}

// Output: The string contains 'codingCourses'.
$searchTerm = 'apple';
$text = 'I like apples and bananas.';

if (str_contains($text, $searchTerm)) {
    echo "The text contains the word '$searchTerm'.";
} else {
    echo "The text does not contain the word '$searchTerm'.";
}

// Output: The text contains the word 'apple'.
$content = 'This sentence contains unwanted words.';
$unwantedWords = ['unwanted', 'bad', 'inappropriate'];

foreach ($unwantedWords as $word) {
    if (str_contains($content, $word)) {
        $content = str_replace($word, '***', $content);
    }
}

echo $content;

// Output: This sentence contains *** words.
$fileName = 'document.pdf';
$allowedExtensions = ['pdf', 'docx', 'txt'];

$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);

if (in_array($fileExtension, $allowedExtensions)) {
    echo 'The file extension is valid.';
} else {
    echo 'Invalid file extension.';
}

// Output: The file extension is valid.