Page MenuHomePhorge
Diviner libphutil Tech Docs xsprintf_callback_example

function xsprintf_callback_example($userdata, &$pattern, &$pos, &$value, &$length)
libphutil Technical Documentation ()

Example xsprintf() callback. When you call xsprintf, you must pass a callback like this one. xsprintf will invoke the callback when it encounters a conversion (like "%Z") in the pattern string.

Generally, this callback should examine $pattern[$pos] (which will contain the conversion character, like 'Z'), escape $value appropriately, and then replace $pattern[$pos] with an 's' so sprintf prints the escaped value as a string. However, more sophisticated behaviors are possible -- particularly, consuming multiple characters to allow for conversions like "%Ld". In this case, the callback needs to substr_replace the entire conversion with 's' and then update $length.

For example implementations, see xsprintf_command(), xsprintf_javascript() and xsprintf_query().

Parameters
wild$userdataArbitrary, optional userdata. This is whatever userdata was passed to @{function:xsprintf}.
string&$patternThe pattern string being parsed.
int&$posThe current character position in the string.
wild&$valueThe value to convert.
int&$lengthThe string length.
Return
wild