Advertisements

wp_sprintf

Definition:
function wp_sprintf( $pattern ) {}

WordPress implementation of PHP sprintf() with filters.

Parameters

  • string $pattern: The string which formatted args are inserted.
  • mixed $args,…: Arguments to be formatted into the $pattern string.

Return values

returns:The formatted string.

Defined filters

  • wp_sprintf
    apply_filters( 'wp_sprintf', $fragment, $arg )

Source code

function wp_sprintf( $pattern ) {

	$args = func_get_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) ) {

				$arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';

				$fragment = str_replace("%{$matches[1]}$", '%', $fragment);

			} else {

				++$arg_index;

				$arg = isset($args[$arg_index]) ? $args[$arg_index] : '';

			}



			// Apply filters OR sprintf

			$_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );

			if ( $_fragment != $fragment )

				$fragment = $_fragment;

			else

				$fragment = sprintf($fragment, strval($arg) );

		}



		// Append to result and move to next fragment

		$result .= $fragment;

		$start = $end;

	}

4139

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: