Skip to content

Instantly share code, notes, and snippets.

@bilalucar
Created February 20, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bilalucar/bf5b517f9512452e3b8b2a876cba83b7 to your computer and use it in GitHub Desktop.
Save bilalucar/bf5b517f9512452e3b8b2a876cba83b7 to your computer and use it in GitHub Desktop.
PSR-3
/**
* Interpolates context values into the message placeholders.
*/
function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val;
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
// a message with brace-delimited placeholder names
$message = "User {username} created";
// a context array of placeholder names => replacement values
$context = array('username' => 'bolivar');
// echoes "User bolivar created"
echo interpolate($message, $context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment