How can special characters, such as square brackets, be properly handled in PHP string manipulation functions like ereg_replace?

Special characters like square brackets can be properly handled in PHP string manipulation functions like ereg_replace by escaping them with a backslash (\) before using them in the function. This ensures that the special characters are treated as literal characters rather than having their special meaning.

$string = "This is [a] test";
$pattern = "/\[/"; //escape the square bracket with a backslash
$replacement = "(";
$new_string = ereg_replace($pattern, $replacement, $string);
echo $new_string; //Output: "This is (a] test"