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').

🌐
PHP
php.net › manual › en › function.str-contains.php
PHP: str_contains - Manual
You should explode the string by whitespace, punctations, ... and check if the resulting array contains your word OR try to test with a RegEx like this: (^|[\s\W])+word($|[\s\W])+ Disclaimer: The RegEx may need some tweaks
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

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,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 (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;
    }
🌐
Sentry
sentry.io › sentry answers › php › how do i check if a string contains a specific word in php?
How do I check if a string contains a specific word in PHP? | Sentry
In contrast to str_contains(), you need to explicitly include a comparator in an if statement, such as $foo === false. For example: if ($ans === false) { echo "The string '$protocol' is NOT in '$url'"; } else { echo "The string '$protocol' is ...
🌐
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!"; } ?>
🌐
ReqBin
reqbin.com › code › php › menoknaq › php-string-contains-example
How to check if a string contains a substring in PHP?
If the string contains the searched ... "false". In PHP, indexes start at 0. As of PHP 8+, you can use the new str_contains($string, $substring) function to check if a string contains the desired substring or word....
🌐
DEV Community
dev.to › ankitvermaonline › how-to-check-if-a-string-contains-specific-words-in-php-1ho
How to check if a string contains specific words in PHP - DEV Community
December 20, 2025 - Check if a string contains a specific word: Use the PHP str_contains() function to determine if a string contains a specific word.
🌐
Uptimia
uptimia.com › home › questions › how to check if a string contains a specific word in php?
How to Check if a String Contains a Specific Word in PHP?
May 20, 2024 - Check if a string contains a specific word in PHP using the strpos() or str_contains() function. Learn how to efficiently search for substring matches in a string.
🌐
W3Docs
w3docs.com › php
How to Check if a String Contains a Specific Word in PHP
You can apply the strpos() function for checking if a string contains a certain word. This function is capable of returning the position of the first occurrence of a substring inside a string.
Find elsewhere
🌐
mixable Blog
mixable.blog › home › php: how to check if a string contains a specific word?
PHP: How to check if a string contains a specific word? | mixable Blog
May 7, 2024 - $string = 'This is a sample string.'; $word = 'sample'; $pattern = '/' . $word . '/i'; if (preg_match($pattern, $string)) { echo 'The string contains the word.'; } else { echo 'The string does not contain the word.'; } PHP 8 introduces the method str_contains().
🌐
Squash
squash.io › how-to-check-if-a-php-string-contains-a-specific-word
How To Check If A PHP String Contains A Specific Word
The strpos() function is a built-in PHP function that can be used to find the position of the first occurrence of a substring within a string. By using this function, we can check if a word exists within a string and perform further actions ...
🌐
Medium
medium.com › @aryanvania03 › how-do-i-check-if-a-string-contains-a-specific-word-785cf333922b
How do I check if a string contains a specific word? | by Daniel Martin | Medium
January 29, 2025 - $string = "The quick brown fox ...____________________________________ The str_contains() function checks if a string contains a substring and returns a boolean value (true or false)....
🌐
TecAdmin
tecadmin.net › check-if-string-contains-specific-word-in-php
How to check if string contains specific word in PHP – TecAdmin
April 26, 2025 - This tutorial will help you to check if a string contains any substring in PHP programming language. For example, you want to run a specific line of code only if an input string contains another substring in it. Here is a sample PHP programme, which will evaluate to true because the main string ...
🌐
STechies
stechies.com › check-string-contains-specific-word-substring-php
How to Check if String Contains Specific Word or Substring in PHP?
<?php $word = "fox"; $mystring = "The quick brown fox jumps over the lazy dog"; // Check if string contains another string $strposition = strpos($mystring, $word); if($strposition !== false){ echo "Word Fount at Position: ".$strposition; } else{ echo "Word Not Found!"; } ?>
🌐
TecAdmin
tecadmin.net › check-string-contains-substring-in-php
How to Check If String Contains a Substring in PHP - TecAdmin
April 26, 2025 - Use the PHP strpos() function to check if a string contains a specific substring or not. Check if string contains any specific word using PHP.
🌐
Steemit
steemit.com › utopian-io › @gentlemanoi › php-how-to-check-if-a-string-contains-a-specific-word-string
PHP : How to check if a string contains a specific word/string — Steemit
February 7, 2018 - Another way is to use regular expression to check if there is a match using preg_match function. This function will return true if there is a match on a specified word, otherwise false. This is the syntax for preg_match: preg_match ( string $pattern , string $subject [, array &$matches [, int ...
🌐
TutorialKart
tutorialkart.com › php › php-check-if-string-contains-specific-word
PHP - Check if string contains specific word
May 7, 2023 - In the following example, we will use strpos() function to check if string contains specific word. ... <?php $str = "Apple is happy."; $word = "Apple"; if (strpos($str, $word) === false) { echo "Word is not present in string."; } else { echo ...
🌐
W3Docs
w3docs.com › php
PHP string "contains"
<?php $string = 'The quick brown fox jumps over the lazy dog'; if (strpos($string, 'fox') !== false) { echo 'The string contains the word fox'; } else { echo 'The string does not contain the word fox'; }
🌐
DEV Community
dev.to › codeanddeploy › how-to-check-if-string-contains-specific-word-in-php-laravel-2nod
How To Check if String Contains Specific Word in PHP Laravel - DEV Community
May 16, 2022 - use Illuminate\Support\Str; $string = 'Laravel is the best PHP framework.'; if(Str::contains($string, 'framework')) { echo 'true'; } For multiple words/strings, you can use an array for the second parameter as you can see below.