How to check if a string contains a specific word with PHP?

Spread the love

Sometimes, we want to check if a string contains a specific word with PHP.

In this article, we’ll look at how to check if a string contains a specific word with PHP.

How to check if a string contains a specific word with PHP?

To check if a string contains a specific word with PHP, we can use the strpos function.

For instance, we write

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

to call strpos with the string to check and the substring we’re searching for.

If it doesn’t return false, then string $a has 'are' in it.

Conclusion

To check if a string contains a specific word with PHP, we can use the strpos function.

Leave a Reply

Your email address will not be published.