How can the preg_match_all function be used to extract values within parentheses in a PHP string?

To extract values within parentheses in a PHP string using preg_match_all, you can create a regular expression pattern that matches the content inside the parentheses. The regular expression should include the opening and closing parentheses as well as a capturing group to extract the desired content. By using preg_match_all function with this regular expression pattern, you can retrieve all the values within parentheses in the input string.

$string = "This is a (sample) string (with) values (in) parentheses";
$pattern = '/\((.*?)\)/';
preg_match_all($pattern, $string, $matches);

print_r($matches[1]);