Skip to content

Instantly share code, notes, and snippets.

@alganet
Created February 11, 2011 14:15
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 alganet/822397 to your computer and use it in GitHub Desktop.
Save alganet/822397 to your computer and use it in GitHub Desktop.
Nice little string formatter
<?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