Created
February 11, 2011 14:15
-
-
Save alganet/822397 to your computer and use it in GitHub Desktop.
Nice little string formatter
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
<?php | |
function stringf($template, array $vars=array()) | |
{ | |
return preg_replace_callback( | |
'/{{(\w+)}}/', | |
function($match) use(&$vars) { return $vars[$match[1]]; }, | |
$template | |
); | |
} | |
print stringf( | |
'Hello, {{name}}. Today is {{day}}', | |
array( | |
'name'=>'Alexandre', | |
'day'=> date('l') | |
) | |
); //Hello, Alexandre. Today is Friday |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment