Skip to content

Instantly share code, notes, and snippets.

Last active November 13, 2019 11:48
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Named param vsprintf()
* Named-Param vsprintf()
* positional-params based on key name, much the same as positional in sprintf()
* @link
* @link
* @param string $str format string - replacements are in %KEY$x format
* where KEY is the array key from $args and x is the sprintf format
* @param array $args key-value associative array of replacements
* @return string
function nvsprintf($str, array $args) {
$i = 1;
foreach ($args as $k => $v) {
$str = str_replace("%{$k}$", "%{$i}$", $str);
return vsprintf($str, array_values($args));
echo nvsprintf('%name$sFoo bar%val$08dxx', array(
'name' => 'BARBAR',
'val' => 33
));//// echos BARBARFoo bar00000033xx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment