What best practices should be followed when using preg_quote() in PHP to avoid errors in regex patterns?

When using preg_quote() in PHP to escape special characters in a string before using it in a regular expression pattern, it is important to remember to specify the delimiter used in the regex pattern as the second parameter to preg_quote(). This helps avoid errors that may occur if the delimiter is not properly escaped. Additionally, it is recommended to store the escaped string in a variable for later use in the regex pattern.

$string = "This is a regex pattern with special characters like ^ and $";
$escaped_string = preg_quote($string, '/');
$pattern = '/^' . $escaped_string . '$/';

// Now you can safely use $pattern in your regex operations