ku234
ku234

Reputation: 71

How to find a word from a sentence using php case insensitive?

I am having difficulty to find a word from a sentence. Using the code below it should return false but its returning true. Can anyone help me, please?

 if(stristr('I live in a farmhouse','farm'))
    {
       echo 'true';
    }
    else
    {
       echo 'false';
    }

Upvotes: 1

Views: 79

Answers (1)

IT goldman
IT goldman

Reputation: 19485

For this mission a Regular Expression (regexp) would come handy. There is \b separator which means "word boundry". So let's see how it goes: (The i at the end means insensitive)

if (preg_match('/\bfarm\b/i', 'I live in a farmhouse')) {
    echo 'true';
} else {
    echo 'false';
}

Upvotes: 6

Related Questions