Created
September 16, 2010 23:44
-
-
Save njones/583393 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Allows "named" parameters for vprintf. You can use one of two ways to name the values | |
* in the string. Mixing and matching in the same string is supported. | |
* | |
* It also throws an error when you don't have a value in the array as expected. | |
* | |
* $a = array("lazy" => "z", "jmp" => "jumps", "qik" => "QUICK", "times" => 10.625); | |
* | |
* The following strings can all be used: %(name)s -- python || %name$s --php | |
* | |
* $b = 'The %(qik)s brown fox %(jmp)s over the la%(lazy)sy dog %(times)d times'; | |
* $c = 'The %qik$s brown fox %jmp$s over the la%lazy$\'z10sy dog %times$1.1f times'; | |
* $d = "The %qik\$s brown fox %(jmp)'.20s over the CRAZY dog %(times)1.2f times"; | |
* | |
* @author Nika Jones | |
* | |
* @copyright 2010 Appcelerator Inc. | |
* | |
* Licensed under the terms of the Apache Public License | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
*/ | |
function kvprintf($_string, $_array) | |
{ | |
return vprintf(preg_replace("/%(?:(?<!\()(([\w-]+)\\$)|\(([\w-]+)\))([a-z0-9\.-\\\'\\\+]+)/e", "'%'.((is_bool(\\\$n=array_search('\\2\\3',unserialize('".serialize(array_keys($_array))."')))?user_error('kvprintf(): Missing argument \'\\2\\3\'',E_USER_WARNING):\\\$n)+1).'\$\\4';", $_string), array_values($_array)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment