wp_sprintf()
WordPress implementation of PHP sprintf() with filters.
1 time — 0.000104 sec (fast) | 50000 times — 0.30 sec (very fast) | PHP 7.1.11, WP 4.9.7
Hooks from the function
Return
String
. The formatted string.
Usage
wp_sprintf( $pattern, ...$args );
- $pattern(string) (required)
- The string which formatted args are inserted.
- ...$args(mixed) (required)
- Arguments to be formatted into the $pattern string.
Examples
#1 Example of a substitute %l
echo wp_sprintf( '%s: %l', 'Prefix', [ 'one', 'two', 'three', 'four' ] ); //> Prefix: one, two, three and four
#2 More examples of format conversions
$pattern = '%d monkeys are sitting on the %s'; echo wp_sprintf( $pattern, 5, 'tree' ); //> 5 monkeys are sitting on the tree $pattern = 'There are %1$d monkeys sitting on the %2$s'; echo wp_sprintf( $pattern, 5, 'tree' ); //> There are 5 monkeys on the tree $pattern = 'There are %1$d monkeys sitting on the %2$s. %1$d monkeys are sitting on the %2$s'; echo wp_sprintf( $pattern, 5, 'tree' ); //> There are 5 monkeys on the tree. there are 5 monkeys on the tree.
Changelog
Since 2.5.0 | Introduced. |
Since 5.3.0 | Formalized the existing and already documented ...$args parameter by adding it to the function signature. |