note 98664 added to function.vsprintf
| From: | strata_ranger at hotmail dot com | Date: | Tue, 29 Jun 2010 18:15:17 +0000 |
| Subject: | note 98664 added to function.vsprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170560@lists.php.net to get a copy of this message | ||
Here's a simple variation on vsprintf() suitable for use with database queries where the
results were retrieved as an associative array keyed by column names.
<?php
function dbsprintf($format, $fields, $row)
// $format - sprintf() compatible format string
// $row - Array containing key/value pairs of data
// $fields - Array containing key names (from $row) that are to be used as arguments
{
// Loop through $fields and insert the corresponding values from $row
foreach($fields as &$value)
{
$value = $row[$value];
} unset($value);
// Format the string and return
return vsprintf($format, $fields);
}
// Some data
$row = Array('id' => '12', 'name' => 'World');
// Outputs "Hello World!"
echo dbsprintf('Hello, %s!', Array('name'), $row);
// Outputs "Hello 12!"
echo dbsprintf('Hello, %s!', Array('id'), $row);
?>
----
Server IP: 209.68.5.5
Probable Submitter: 71.54.198.140
----
Manual Page -- http://www.php.net/manual/en/function.vsprintf.php
Edit -- https://master.php.net/note/edit/98664
Del: integrated -- https://master.php.net/note/delete/98664/integrated
Del: useless -- https://master.php.net/note/delete/98664/useless
Del: bad code -- https://master.php.net/note/delete/98664/bad+code
Del: spam -- https://master.php.net/note/delete/98664/spam
Del: non-english -- https://master.php.net/note/delete/98664/non-english
Del: in docs -- https://master.php.net/note/delete/98664/in+docs
Del: other reasons-- https://master.php.net/note/delete/98664
Reject -- https://master.php.net/note/reject/98664
Search -- https://master.php.net/manage/user-notes.php