Avatar ·

How to check if a string contains a specific word in PHP?

Hi everyone! I'm new to PHP and writing a small script for filtering comments on a website. I need to check if the comment text contains certain forbidden words, for example "spam" or "advertising". \n\nThe problem is that I can't find the right way to do this. I tried using the == operator, but it doesn't work with strings. Then I found the strpos() function, but for some reason it finds the word even inside other words. For example, when I search for the word "advertising", it finds it in the word "readvertising" — that's wrong. Also, I don't understand how to handle case sensitivity: the word "Spam" with a capital letter should also be considered a match.\n\nHere is my current code that doesn't work as expected:\n\n```php\n$text = "This is spam, delete it!";\n$word = "spam";\n\nif (strpos($text, $word) !== false) {\n```