★ wanayoo — archive 1999 http://fr.php.net/manual/de/function.vsprintf.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<vprintfwordwrap>
view the version of this page
Last updated: Fri, 18 Nov 2005

vsprintf

(PHP 4 >= 4.1.0, PHP 5)

vsprintf -- Gibt einen formatierten String zurück

Beschreibung

string vsprintf ( string format, array args )

Gibt Array-Werte als formatierten String entsprechend der Formatanweisung (siehe Beschreibung der Formatierungsmöglichkeiten der Funktion sprintf()) zurück.

Verhält sich wie sprintf(), akzeptiert jedoch ein Array anstelle mehrerer Werte als Parameter.

Siehe auch sprintf() und vprintf()



add a note add a note User Contributed Notes
vsprintf
tbS dot P dot A dot M at S dot U dot K dot Staylorbarstow dot com
01-Mar-2005 08: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
23-May-2004 03: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
30-Jan-2004 06: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 03: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);
}
?>

<vprintfwordwrap>
 Last updated: Fri, 18 Nov 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Nov 26 05:21:09 2005 CET