Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading