What are some best practices for creating logic to check for specific words in PHP input fields?

When creating logic to check for specific words in PHP input fields, it is important to sanitize and validate the user input to prevent any potential security risks. One way to achieve this is by using PHP's strpos() function to check if the input contains the specific word or phrase. Additionally, you can use regular expressions to search for patterns within the input.

// Sanitize and validate user input
$input = $_POST['input_field'];

// Check if input contains specific word
$specific_word = 'example';
if (strpos($input, $specific_word) !== false) {
    echo 'Input contains the specific word.';
} else {
    echo 'Input does not contain the specific word.';
}