🌐
PHP
php.net › manual › en › function.str-contains.php
PHP: str_contains - Manual
For instance, ド in Windows-1252 will match ド from the above string. So it's still best to convert the encoding of the parameters to be the same first. But, if the character set isn't known/can't be detected and you have no choice but to deal with dirty data, this is probably the simplest solution. ... <?php // Polyfill for PHP 4 - PHP 7, safe to utilize with PHP 8 if (!function_exists('str_contains')) { function str_contains (string $haystack, string $needle) { return empty($needle) || strpos($haystack, $needle) !== false; } }
🌐
W3Schools
w3schools.com › php › func_string_str_contains.asp
PHP str_contains() Function
Checking the existence of an empty string will always return true: <?php $txt = "PHP"; var_dump(str_contains($txt, "")); // bool(true) ?> Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Schools
w3schools.com › php › php_string.asp
PHP Strings
In PHP, strings are surrounded by either double quotes, or single quotes.
🌐
W3Schools
w3schools.com › php › php_string_functions.asp
PHP String Functions
$txt = "I really love PHP!"; var_dump(str_contains($txt, "love")); Try it Yourself » · Note: This function performs a case-sensitive search. The following example will return a boolean false, because "Love" is not found in the main string:
🌐
W3Resource
w3resource.com › php-exercises › php-string-exercise-3.php
PHP String Exercise: Check whether a string contains a specific string - w3resource
Check whether the said string contains the string 'jumps'. ... <?php // Define the input string $str1 = 'The quick brown fox jumps over the lazy dog.'; // Check if the word "jumps" is present in the string if (strpos($str1,'jumps') !== false) { // If present, echo a message indicating its presence echo 'The specific word is present.'; } else { // If not present, echo a message indicating its absence echo 'The specific word is not present.'; } ?>
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php str_contains
PHP str_contains - PHP Tutorial
April 7, 2025 - As mentioned earlier, the str_contains() function uses the case-sensitive search for checking if a substring is in a string. For example: <?php $haystack = 'PHP is cool.'; $needle = 'Cool'; $result = str_contains($haystack, $needle) ?
🌐
Codecademy
codecademy.com › docs › php › string functions › str_contains()
PHP | String Functions | str_contains() | Codecademy
July 1, 2023 - The str_contains() function performs a case sensitive search for a given substring within a string.
🌐
W3Docs
w3docs.com › php
PHP string "contains"
Here's an example of how you can use strpos() to check if a string contains another string: <?php $string = 'The quick brown fox jumps over the lazy dog'; if (strpos($string, 'fox') !== false) { echo 'The string contains the word fox'; } else ...
🌐
W3Docs
w3docs.com › php
How to Check if a String Contains a Specific Word in PHP
There is a new function str_contains() in PHP 8 that provides the same functionality. <?php $word = 'fox'; $myString = 'The quick brown fox jumps over the lazy dog'; // Test whether the string contains the word if (str_contains($myString, $word)) { echo 'Word Found!'; } else { echo 'Word Not Found!'; }
Find elsewhere
🌐
Swancreekestates
swancreekestates.ca › wp-content › uploads › citral-source-hpri › 5a37e4-string-contains-php-w3schools
string contains php w3schools
The typical way to check if a string is contained in another is mostly done by using the functions strpos or strstr.Because this feature is such a common use-case in almost every project, it should deserve its own dedicated function: str_contains. preg_replace - w3schools php str_replace .
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_contains-function
PHP str_contains() Function - GeeksforGeeks
September 9, 2021 - 'is' : 'is not'; echo "The word {$word} {$result} present in the sentence \"{$sentence}\" "; ?> From the above, we have seen how to find the substring in a given string by using the str_contains() function & the return value will be the boolean.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-if-a-string-contains-a-specific-word-in-php.php
How to Check If a String Contains a Specific Word in PHP
<?php $word = "fox"; $mystring = "The quick brown fox jumps over the lazy dog"; // Test if string contains the word if(strpos($mystring, $word) !== false){ echo "Word Found!"; } else{ echo "Word Not Found!"; } ?>
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-check-if-a-string-contains-a-substring-in-php
How to check if a String Contains a Substring in PHP ? - GeeksforGeeks
July 23, 2025 - <?php $text = "Develop with PHP"; if (preg_match("/PHP/", $text)) { echo "Match found using regex."; } ?> Use str_contains() for simple checks (if PHP 8+).
Top answer
1 of 16
8100

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

Please note: The str_contains function will always return true if the $needle (the substring to search for in your string) is empty.

$haystack = 'Hello';
$needle   = '';

if (str_contains($haystack, $needle)) {
    echo "This returned true!";
}

You should first make sure the $needle (your substring) is not empty.

$haystack = 'How are you?';
$needle   = '';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

It's also worth noting that the new str_contains function is case-sensitive.

$haystack = 'How are you?';
$needle   = 'how';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

2 of 16
760

You could use regular expressions as it's better for word matching compared to strpos, as mentioned by other users. A strpos check for are will also return true for strings such as: fare, care, stare, etc. These unintended matches can simply be avoided in regular expression by using word boundaries.

A simple match for are could look something like this:

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}

On the performance side, strpos is about three times faster. When I did one million compares at once, it took preg_match 1.5 seconds to finish and for strpos it took 0.5 seconds.

Edit: In order to search any part of the string, not just word by word, I would recommend using a regular expression like

$a = 'How are you?';
$search = 'are y';
if(preg_match("/{$search}/i", $a)) {
    echo 'true';
}

The i at the end of regular expression changes regular expression to be case-insensitive, if you do not want that, you can leave it out.

Now, this can be quite problematic in some cases as the $search string isn't sanitized in any way, I mean, it might not pass the check in some cases as if $search is a user input they can add some string that might behave like some different regular expression...

Also, here's a great tool for testing and seeing explanations of various regular expressions Regex101

To combine both sets of functionality into a single multi-purpose function (including with selectable case sensitivity), you could use something like this:

function FindString($needle,$haystack,$i,$word)
{   // $i should be "" or "i" for case insensitive
    if (strtoupper($word)=="W")
    {   // if $word is "W" then word search instead of string in string search.
        if (preg_match("/\b{$needle}\b/{$i}", $haystack)) 
        {
            return true;
        }
    }
    else
    {
        if(preg_match("/{$needle}/{$i}", $haystack)) 
        {
            return true;
        }
    }
    return false;
    // Put quotes around true and false above to return them as strings instead of as bools/ints.
}

One more thing to take in mind, is that \b will not work in different languages other than english.

The explanation for this and the solution is taken from here:

\b represents the beginning or end of a word (Word Boundary). This regex would match apple in an apple pie, but wouldn’t match apple in pineapple, applecarts or bakeapples.

How about “café”? How can we extract the word “café” in regex? Actually, \bcafé\b wouldn’t work. Why? Because “café” contains non-ASCII character: é. \b can’t be simply used with Unicode such as समुद्र, 감사, месяц and .

When you want to extract Unicode characters, you should directly define characters which represent word boundaries.

The answer: (?<=[\s,.:;"']|^)UNICODE_WORD(?=[\s,.:;"']|$)

So in order to use the answer in PHP, you can use this function:

function contains($str, array $arr) {
    // Works in Hebrew and any other unicode characters
    // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
    // Thanks https://www.phpliveregex.com/
    if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|$)/', $str)) return true;
}

And if you want to search for array of words, you can use this:

function arrayContainsWord($str, array $arr)
{
    foreach ($arr as $word) {
        // Works in Hebrew and any other unicode characters
        // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
        // Thanks https://www.phpliveregex.com/
        if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|$)/', $str)) return true;
    }
    return false;
}

As of PHP 8.0.0 you can now use str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always"
        return true;
    }
🌐
TutorialsPoint
tutorialspoint.com › php-8-using-str-contains-to-check-if-a-string-contains-a-substring
PHP 8 – Using str_contains() to check if a string contains a substring
str_contains(string $haystack, string $needle): bool · <?php if (str_contains('great reading tutorial', 'tutorial')) { var_dump('Tutorial has been found'); } ?>
🌐
Nabilhassen
nabilhassen.com › search-for-a-string-inside-another-string-in-php
PHP: Check if a string contains a substring - Nabil Hassen
November 13, 2025 - str_contains() is the recommended modern function and the clearest way to find in string operations. For PHP versions before 8 or when you need the exact position of a match, use strpos().
🌐
Tutorialspoint
tutorialspoint.com › php › php_str_contains_function.htm
PHP String str_contains() Function
Now the below code uses the str_contains() function to check multiple substrings in a single string. So the code iterates over an array of substrings and checks each one in the main string.
🌐
W3Schools
w3schools.com › php › func_string_strstr.asp
PHP strstr() Function
affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_lengths fetch_object fetch_row field_count field_seek get_charset get_client_info get_client_stats get_client_version get_connection_stats get_host_info get_proto_info get_server_info get_server_version info init insert_id kill more_results multi_query next_result options ping poll prepare query real_connect real_escape_string real_query reap_async_query refresh rollback select_db set_charset set_local_infile_handler sqlstate ssl_set stat stmt_init thread_id thread_safe use_result warning_count PHP Network
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-check-if-a-string-contains-a-specific-character-in-php
How to check if a String contains a Specific Character in PHP ? - GeeksforGeeks
July 23, 2025 - The string contains the character 'o'. The preg_match() function is used to perform a regular expression match. It allows for more complex pattern matching, including checking for the presence of specific characters using regular expressions.