note 110666 added to function.vsprintf
| From: | JosefKufner | Date: | Tue, 20 Nov 2012 23:51:58 +0000 |
| Subject: | note 110666 added to function.vsprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192047@lists.php.net to get a copy of this message | ||
<?php
/**
* Like vsprintf, but accepts $args keys instead of order index.
* Both numeric and strings matching /[a-zA-Z0-9_-]+/ are allowed.
*
* Example: vskprintf('y = %y$d, x = %x$1.1f', array('x' => 1, 'y'
=> 2))
* Result: 'y = 2, x = 1.0'
*
* $args also can be object, then it's properties are retrieved
* using get_object_vars().
*
* '%s' without argument name works fine too. Everything vsprintf() can do
* is supported.
*
* @author Josef Kufner <jkufner(at)gmail.com>
*/
function vksprintf($str, $args)
{
if (is_object($args)) {
$args = get_object_vars($args);
}
$map = array_flip(array_keys($args));
$new_str = preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',
function($m) use ($map) { return $m[1].'%'.($map[$m[2]] + 1).'$'; },
$str);
return vsprintf($new_str, $args);
}
----
Server IP: 147.32.160.17
Probable Submitter: 78.102.220.76
----
Manual Page -- http://www.php.net/manual/en/function.vsprintf.php
Edit -- https://master.php.net/note/edit/110666
Del: integrated -- https://master.php.net/note/delete/110666/integrated
Del: useless -- https://master.php.net/note/delete/110666/useless
Del: bad code -- https://master.php.net/note/delete/110666/bad+code
Del: spam -- https://master.php.net/note/delete/110666/spam
Del: non-english -- https://master.php.net/note/delete/110666/non-english
Del: in docs -- https://master.php.net/note/delete/110666/in+docs
Del: other reasons-- https://master.php.net/note/delete/110666
Reject -- https://master.php.net/note/reject/110666
Search -- https://master.php.net/manage/user-notes.php