-
-
Save bohwaz/ad6d31568ae37df4005f44e3487c69ea to your computer and use it in GitHub Desktop.
Named param vsprintf()
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 | |
/** | |
* Named-Param vsprintf() | |
* | |
* positional-params based on key name, much the same as positional in sprintf() | |
* | |
* @link http://php.net/manual/en/function.sprintf.php | |
* @link http://www.php.net/manual/en/function.vsprintf.php | |
* | |
* @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); | |
$i++; | |
} | |
return vsprintf($str, array_values($args)); | |
} | |
////example | |
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