What are the potential pitfalls of not escaping variables in PHP, especially when using them in regular expressions?

Not escaping variables in PHP, especially when using them in regular expressions, can lead to security vulnerabilities such as injection attacks. To prevent this, always escape variables before using them in regular expressions by using the preg_quote() function.

$user_input = $_POST['user_input'];
$escaped_input = preg_quote($user_input, '/');
// Now $escaped_input can be safely used in a regular expression