wp_sprintf()WP 2.5.0

Variant of the PHP function sprintf() from WordPress. Supports %l (list) in the passed format.

Gets a formatted string created based on the specified format: see the description of the PHP function sprintf().

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

Returns

String. Text created based on the specified format (pattern).

Usage

wp_sprintf( $pattern, $args... );
$pattern(string) (required)

String format in which the parameters $args will be used.

%l (list) - a new placeholder that does not exist in sprintf(). An array with values that will be written with commas should be passed in place of this placeholder (the last comma will be replaced with "and").

%l placeholder is passed through the filter wp_sprintf, to which the function wp_sprintf_l() is attached in WP.

$args(mixed) (required)
,... Parameters that will be used in the format string $pattern.

Examples

1

#1 Example of a substitute %l

echo wp_sprintf( '%s: %l', 'Prefix', [ 'one', 'two', 'three', 'four' ] );
//> Prefix: one, two, three and four
1

#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.

wp_sprintf() code WP 6.8.3

function wp_sprintf( $pattern, ...$args ) {
	$len       = strlen( $pattern );
	$start     = 0;
	$result    = '';
	$arg_index = 0;

	while ( $len > $start ) {
		// Last character: append and break.
		if ( strlen( $pattern ) - 1 === $start ) {
			$result .= substr( $pattern, -1 );
			break;
		}

		// Literal %: append and continue.
		if ( '%%' === substr( $pattern, $start, 2 ) ) {
			$start  += 2;
			$result .= '%';
			continue;
		}

		// Get fragment before next %.
		$end = strpos( $pattern, '%', $start + 1 );
		if ( false === $end ) {
			$end = $len;
		}
		$fragment = substr( $pattern, $start, $end - $start );

		// Fragment has a specifier.
		if ( '%' === $pattern[ $start ] ) {
			// Find numbered arguments or take the next one in order.
			if ( preg_match( '/^%(\d+)\$/', $fragment, $matches ) ) {
				$index    = $matches[1] - 1; // 0-based array vs 1-based sprintf() arguments.
				$arg      = isset( $args[ $index ] ) ? $args[ $index ] : '';
				$fragment = str_replace( "%{$matches[1]}$", '%', $fragment );
			} else {
				$arg = isset( $args[ $arg_index ] ) ? $args[ $arg_index ] : '';
				++$arg_index;
			}

			/**
			 * Filters a fragment from the pattern passed to wp_sprintf().
			 *
			 * If the fragment is unchanged, then sprintf() will be run on the fragment.
			 *
			 * @since 2.5.0
			 *
			 * @param string $fragment A fragment from the pattern.
			 * @param string $arg      The argument.
			 */
			$_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );

			if ( $_fragment !== $fragment ) {
				$fragment = $_fragment;
			} else {
				$fragment = sprintf( $fragment, (string) $arg );
			}
		}

		// Append to result and move to next fragment.
		$result .= $fragment;
		$start   = $end;
	}

	return $result;
}