How can I check using PHP regular expressions, whether my string variable $str contains the word 'cat' but does not contain the word 'dog'.
- Case 1: $str = "My pet: parrot" -> Output: false
- Case 2: $str = "My pet: dog and cat" -> Output: false
- Case 3: $str = "My pet: cat" -> Output: true
I have tried this and it works but I was wondering if there was a single regular expression to do it
$str = "My pet: dog and cat";
if (preg_match('/(\bdog\b)/', $str) && !preg_match('/(\bcat\b)/', $str)) echo 'TRUE';
else echo 'FALSE';