What are the potential pitfalls of using the strpos function to check for bad words in a message?

Using the strpos function to check for bad words in a message may not be effective as it only checks for the presence of a substring, not the entire word. This can lead to false positives if a legitimate word contains a banned word as a substring. To solve this issue, you can use the preg_match function with word boundaries to ensure that the entire word is matched.

$message = "This is a message with a bad word like crap in it.";
$bad_words = array("crap", "badword", "offensive");

foreach ($bad_words as $bad_word) {
    if (preg_match("/\b$bad_word\b/i", $message)) {
        echo "Message contains a bad word: $bad_word";
        break;
    }
}