What is the purpose of using preg_match in PHP and what are the potential pitfalls when dealing with multiple parentheses in a string?

When using preg_match in PHP, the purpose is to search a string for a specific pattern using a regular expression. When dealing with multiple parentheses in a string, it is important to properly escape the parentheses to avoid any unintended behavior or errors in the regular expression.

$string = "This is a (sample) string with (multiple) parentheses.";

// To match strings within parentheses, escape the parentheses
$pattern = "/\((.*?)\)/";

if (preg_match_all($pattern, $string, $matches)) {
    print_r($matches[1]); // Output: Array ( [0] => sample [1] => multiple )
}