I have the following line of code:
$message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body);
This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username.
I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets.
$message = preg_replace_callback(
"/\{\{([a-zA-Z_-]+)\}\}/",
function($match){
error_log($match[0]);
return $$match[0];
},
$body
);
Any help greatly appreciated.
$match[1]for 1st capture group containing your variable name.error_log($match[0])seems to be used to debug. You could study about using xdebug (xdebug.org). It's easy and much better to debug than this.{{username}}then$usernameis undefined in the function. You could put them all in an associative array, and then make that array available via theusestatement. This way, you're also essentially building a whitelist of allowed vars, so things like{{this}}won't be exploitable.