note 110622 deleted from function.sprintf by aharvey
| From: | aharvey@php.net | Date: | Wed, 14 Nov 2012 08:58:23 +0000 |
| Subject: | note 110622 deleted from function.sprintf by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191924@lists.php.net to get a copy of this message | ||
Note Submitter: qeremy [atta] gmail [dotta] com
----
<?php
// Warning error, outputs: NULL, so nothing to display
echo sprintf("%d crazy %s in the garden.\n", 1);
?>
I think this could be a solution for this error: "Warning: sprintf(): Too few arguments in
/var/www/..."
<?php
function sprinter($text) {
preg_match_all('~(%[a-z])~', $text, $m);
if (isset($m[1]) && !empty($m[1])) {
$args = array_slice(func_get_args(), 1);
$keys = $vals = array();
foreach ($args as $arg) {
$keys[] = sprintf('/%s/', array_shift($m[1]));
$vals[] = $arg;
}
return preg_replace($keys, $vals, $text, 1, $count);
}
}
// No error, outputs: 1 crazy %s in the garden.
echo sprinter("%d crazy %s in the garden.\n", 1);
// Normally no error, outputs: 1 crazy frog in the garden.
echo sprinter("%d crazy %s in the garden.\n", 1, "frog");
?>