PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

wordwrap> <vprintf
Last updated: Fri, 09 May 2008

view this page in

vsprintf

(PHP 4 >= 4.0.7, PHP 5)

vsprintf — Return a formatted string

Description

string vsprintf ( string $format , array $args )

Operates as sprintf() but accepts an array of arguments, rather than a variable number of arguments.

Parameters

format

See sprintf() for a description of format .

args

Return Values

Return array values as a formatted string according to format (which is described in the documentation for sprintf()).

See Also



wordwrap> <vprintf
Last updated: Fri, 09 May 2008
 
add a note add a note User Contributed Notes
vsprintf
iisys_ at hotmail dot com
19-Nov-2006 10:29
I've made this simple function for more compatibility with different applications.
As arguments, it can accept an array or single arguments.

<?php
function asprintf() {
   
$a = func_get_args();
   
$format = array_shift($a);
    if (
is_array($a[0])) {
       
$a = $a[0];
    }
    return
vsprintf($format, $a);
}

# Examples.

$sql_arguments = array('fields' => '*', 'table' => 'users', 'field' => 'user', 'value' => 'exploited');

echo
asprintf("SELECT %s FROM %s WHERE %s = '%s'", $sql_arguments);
# Would output: SELECT * FROM users WHERE user = 'exploited'

echo asprintf("SELECT %s FROM %s WHERE %s = '%s'", '*', 'users', 'user', 'exploited');
# Would output the same.
?>
Roadster
30-Sep-2006 07:44
Please note: The same functionality (sortof) can be attained between version 4.0.4 and 4.1.0 using call_user_func_array.

Example:

call_user_func_array("sprintf", $arg)

First element of $arg is the format. This rescued me in a situation where version 4.1.0 wasn't available.
jon at ardentcreative dot co dot uk
17-Feb-2006 03:56
This can be used for quick and dirty internationalization:

<?php
$GLOBALS
['strings']['example'] = "There are %d people.";

// Loads a phrase from the translations list in lang/$lang/phrases.php
function t() {
   
$args = func_get_args();
   
$nArgs = func_num_args();
   
   
$phrase = array_shift($args);
   
$nArgs--;
   
    include_once(
"../lang/" . lang() . "/phrases.php");
    if (isset(
$GLOBALS['strings'][$phrase])) {
        return
vsprintf($GLOBALS['strings'][$phrase], $args);
    } else {
        return
'<span style="color: #ff0000">Untranslated string: ' . $phrase . '</span>';
    }
}
?>
toneee at g mail dot com
19-Dec-2005 09:27
I found this function to be useful for formatting sql queries.

I do something like this:

function sql_build($template, $params = array()) {
  global $sql_templates;
  if (isset($sql_templates[$template])) {
    $sql = vsprintf($sql_templates[$template], $params);
    return $sql;
  }
  return false;
}

// Fetch list of contacts, for a given section id
$sql_templates['contacts_by_section'] = <<<ENDSQL
select
  id,
  name,
  email, 
  address,
  photo_id
from
  contacts
where
  section_id = %d
ENDSQL;

You also give yourself an added layer of security on the sql due to the sprintf formatting. For example, using %d will not allow any sql injection for that parameter.
tbS dot P dot A dot M at S dot U dot K dot Staylorbarstow dot com
01-Mar-2005 11:49
Simple but useful routine:

<?php
function vsprintf_iter($fmt,$data) {
    if (!
is_array($data)) return false;
   
$ret = '';
    foreach (
$data as $d) {
       
$ret .= vsprintf($fmt,$d);
    }
    return
$ret;
}
?>
jed at NOSPAM dot jed dot bz
22-May-2004 06:48
vsprintf() accepts arrays with any keys, so the array_shift() technique is unnecessary when writing a printf-type function. Any parameters you require are easily unset from the array you retrieve with func_get_args():

<?php

function mysprintf($format) {
   
$args = func_get_args();
    unset(
$args[0]); /* get rid of "$format" */
   
return vsprintf($format, $args);
}

/* I use this technique in production code as follows: */
function logf($target, $string) {
   
$args = func_get_args();
    unset(
$args[0], $args[1]);
   
fprintf($GLOBALS['config']['logtargets'][$target],
       
"[%s] %s\n", date('H:i'), wordwrap(vsprintf($string, $args), 75, '\n\r '));
}

/* e.g.:
    logf(DEBUG, "Oops! %s", mysql_error());
 */

?>

array_shift() and other costly array operations aren't required, as far as I know. I could be wrong.
drm at melp dot nl
29-Jan-2004 09:02
When combining with func_get_args as above, you might want to consider using array_shift:

<?
function someFunc () {
   $args = func_get_args ();
   $format = array_shift ( $args );
   // do something
   return vsprintf ( $format, $args );
}
?>

keeps it clean ;)
samviseNOSPAM at hobbitonNOSPAM dot it
02-Jul-2002 06:55
i wrote a short function that make use of vsprintf. It is useful in first coding/debugging of php scripts, because it is called like a simple printf and it display in italic (you can change it of course ;-) ) every debug messages, making it easy to remove them when your code is ready.

here goes :

<?php
function printd() {
       
$debug_array=func_get_args();
       
$debug_numargs=func_num_args();
       
$debug_fmt=$debug_array[0];
        for (
$i = 0 ; $i < $debug_numargs ; $i++ )
               
$debug_array[$i] = $debug_array[$i+1];
       
$debug_text=vsprintf($debug_fmt, $debug_array);
       
printf("--> debug <i>%s</i>\n",
               
$debug_text);
}
?>

wordwrap> <vprintf
Last updated: Fri, 09 May 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites