How Do I Check If A String Contains A Specific Word?

In PHP, you can use the strpos() function to check if a string contains a specific word. Here’s an example:

$string = "This is a sample string.";

if (strpos($string, "sample") !== false) {
    echo "The string contains the word 'sample'.";
} else {
    echo "The string does not contain the word 'sample'.";

The strpos() function returns the position of the first occurrence of the word within the string. If the word is not found, it returns false. To perform a case-insensitive search, you can use the stripos() function instead.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!