What are best practices for handling and sanitizing user input when using regular expressions in PHP?

When using regular expressions in PHP, it is important to sanitize user input to prevent malicious code injection. One common approach is to use the `preg_quote()` function to escape special characters in the user input before using it in a regular expression. Additionally, using functions like `filter_var()` with the `FILTER_SANITIZE_STRING` filter can help sanitize user input.

// Sanitize user input before using it in a regular expression
$user_input = $_POST['user_input'];
$sanitized_input = preg_quote($user_input);

// Use the sanitized input in a regular expression
if (preg_match("/^pattern$/", $sanitized_input)) {
    // Do something
}