What are the best practices for escaping special characters like square brackets in regular expressions when working with PHP?

When working with regular expressions in PHP, special characters like square brackets need to be escaped using a backslash (\) to ensure they are treated as literal characters and not as part of the regular expression syntax. This can be done manually by adding a backslash before each special character, or by using the preg_quote() function which automatically escapes all special characters in a given string.

// Manually escaping special characters
$pattern = '/\[special\]/';

// Using preg_quote() to automatically escape special characters
$pattern = '/'.preg_quote('[special]').'/';