Page MenuHomePhabricator

function xsprintf($callback, $userdata, $argv)
libphutil Technical Documentation ()

Parse a sprintf()-style format string in an extensible way.

This method allows you to build a function with sprintf() semantics but custom conversions for different datatypes. Three examples are jsprintf() (which builds JavaScript strings), qsprintf() (which builds MySQL strings), and csprintf() (which builds command line strings).

To build a new xsprintf-family function, provide a callback which conforms to the specification described in xsprintf_callback_example(). The callback will be invoked each time a conversion (like "%Z") is encountered in the string. For instance, if you call xsprintf() like this...

$result = xsprintf(
  'xsprintf_callback_example',
  $userdata = null,
  array(
    'The %M is made of %C.',
    'moon',
    'cheese',
  ));

...the callback will be invoked twice, at string positions 5 ("M") and 19 ("C"), with values "moon" and "cheese" respectively.

Parameters
string$callbackThe name of a callback to pass conversions to.
wild$userdataOptional userdata to pass to the callback. For @{function:qsprintf}, this is the database connection.
list$argvList of arguments, with the `sprintf()` pattern in position 0.
Return
stringFormatted string.