How can PHP developers effectively escape special characters like square brackets when creating search patterns for text manipulation?

When creating search patterns for text manipulation in PHP, developers can effectively escape special characters like square brackets by using the preg_quote() function. This function escapes any characters that have special meaning in regular expressions, ensuring that they are treated as literal characters in the search pattern.

$search_pattern = '[example]';
$escaped_pattern = preg_quote($search_pattern, '/');
$text = 'This is an [example] of text manipulation.';
$matches = [];
preg_match('/' . $escaped_pattern . '/', $text, $matches);
print_r($matches);