Home > Software design >  How to find a word from a sentence using php case insensitive?
How to find a word from a sentence using php case insensitive?

Time:07-09

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';
    }

CodePudding user response:

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';
}
  •  Tags:  
  • php
  • Related