note 110622 added to function.sprintf

From: Date: Wed, 14 Nov 2012 08:30:40 +0000
Subject: note 110622 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-191923@lists.php.net to get a copy of this message
<?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"); ?> ---- Server IP: 95.130.168.26 Probable Submitter: 78.186.57.165 ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/110622 Del: integrated -- https://master.php.net/note/delete/110622/integrated Del: useless -- https://master.php.net/note/delete/110622/useless Del: bad code -- https://master.php.net/note/delete/110622/bad+code Del: spam -- https://master.php.net/note/delete/110622/spam Del: non-english -- https://master.php.net/note/delete/110622/non-english Del: in docs -- https://master.php.net/note/delete/110622/in+docs Del: other reasons-- https://master.php.net/note/delete/110622 Reject -- https://master.php.net/note/reject/110622 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191923) next »