Created
February 20, 2017 08:42
Star
You must be signed in to star a gist
PSR-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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