note 110666 modified in function.vsprintf by danbrown

From: Date: Thu, 06 Dec 2012 14:14:16 +0000
Subject: note 110666 modified in function.vsprintf by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192308@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); } ?> --was-- <?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); } http://php.net/manual/en/function.vsprintf.php

« previous php.notes (#192308) next »