Biology Forums - Study Force

Science-Related Homework Help Computer Studies Topic started by: 5seal on Jun 29, 2018



Title: How do I check if a string contains a specific word in PHP?
Post by: 5seal on Jun 29, 2018
Let's say:

Code:
$a = 'I am happy?';

if ($a contains 'happy')
    echo 'true';

How is this done?


Title: Re: How do I check if a string contains a specific word in PHP?
Post by: habiba on Jun 29, 2018
Try this:

Code:
$a = 'I am happy?';

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


Title: Re: How do I check if a string contains a specific word in PHP?
Post by: 5seal on Jun 29, 2018
That works perfectly!

What if we want "am" and "happy"? How do I adjust the code above? :thinking:


Title: Re: How do I check if a string contains a specific word in PHP?
Post by: habiba on Jul 19, 2018
Code:
if (strpos($a, 'happy') !== false || strpos($a, 'am') !== false) {
    echo 'true';
}