$res = [
"{val1}" => "ipsum",
"{val2}" => "amet",
"{val3}" => "elit"
];
$mask = "Lorem {val1} dolor sit {val2}, consectetur adipisicing {val3}.";
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit.";
preg_match_all("/({.*?})/", $mask, $mask_matches);
$re = '/^' . preg_replace("/\\\{.*?\\\}/", '(.*?)', preg_quote($mask, '/')) . '$/';
preg_match($re, $str, $str_matches);
array_shift($str_matches);
$result = array_combine($mask_matches[1], $str_matches);
print_r($result);
Array
(
[{val1}] => ipsum
[{val2}] => amet
[{val3}] => elit
)
$str1 = 'Lorem {val1} dolor sit {val2}, consectetur adipisicing {val3}';
$str2 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit';
$words1 = explode(' ', $str1);
$words2 = explode(' ', $str2);
$items = count($words1);
$result = [];
for($i = 0; $i< $items; $i++) {
if(preg_match('/{\w*}/', $words1[$i])) {
$result["$words1[$i]"] = $words2[$i];
}
}
print_r($result);
$s1 = 'Lorem {val1} dolor sit {val2}, consectetur adipisicing {val3}.';
$s2 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$patterns = [];
$pattern2 = preg_replace_callback('|\{([a-z0-9]+)\}|', function($m) use (&$patterns) {
$patterns[] = $m[1];
return '(.*)';
}, $s1);
preg_match('|' . $pattern2 . '|', $s2, $matches);
$res = array_combine($patterns, array_slice($matches, 1));
var_dump($res);