It makes not much sense to extract the variable names out of the string - if the string changes, the names would change, too. Therefore you need to make the regular expression pretty fitting to this specific case so not to introduce unexpected variables.
Also the variables you intend to extract need initialization before doing the extraction.
Some example code (Demo):
$string = 'Main: (555) 551-0341 Pharmacy: (555) 551-4304';
$main = $pharmacy = null;
foreach(
preg_match_all(
'/(Main|Pharmacy): (\(555\) 551-\d{4})/i', $string, $m, PREG_SET_ORDER
)
? $m
: []
as $p
) {
$p[1] = strtolower($p[1]);
$$p[1] = $p[2];
}
var_dump($main, $pharmacy);
Example output:
string(14) "(555) 551-0341"
string(14) "(555) 551-4304"