In PHP, you cannot get the variable name (unless using unreliable hacky methods). But as @samayo mentioned, this is usually handled by a template engine.
If you want to mange this yourself, you can do the following:
$array = [
'tag' => 'a',
'text' => 'b'
];
$html = '<html>
{{tag}}
<body>
{{text}}
</body>
</html>';
Now you can use a couple of methods to replace this, usually done by regex:
preg_match_all("/.*({{(.*)}})/", $html, $result);
Outputting:
$result = array(
0 => array(
0 => '{{tag}}'
1 => '{{text}}'
)
1 => array(
0 => '{{tag}}'
1 => '{{text}}'
)
2 => array(
0 => 'tag'
1 => 'text'
)
)
You could also directly replace the data with preg_replace()
With this, you have all the data you would need to replace text.
$result[2][0] is referring to $array[key] ($array[$result[2][0]]))replacement definition.
This is how template engines usually work.