只取括号内的数字(brackets)

Only take numbers inside parentheses (brackets)

使用下面的代码,

$string = 'd.c part2 15245 (30)';
$string2 = preg_replace('/[^0-9]/', '', $string);
echo $string2;

输出:21524530

如何只取括号内的数字

所以输出是30?

使用preg_match:

$string = 'd.c part2 15245 (30)';
preg_match('/\((\d+)\)/', $string, $match);
print_r($match);

输出:

Array
(
    [0] => (30)
    [1] => 30
)
$re = '/\((\d+)\)/ix';
$str = 'd.c part2 15245 (30)';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);