src/Str.php
public function _format($format, $placeholders = [], $default = '')
	{
		$placeholders = (array) $placeholders;
		$matches = [];
		if(!preg_match_all('/\{([^{}]*)\}+/u', $format, $matches))
			return sprintf($format, ...array_values($placeholders));

		ksort($placeholders, SORT_NATURAL);
		$placeholders['__default__'] = $default;
		$positions = array_flip(array_keys($placeholders));

		$type_specifiers = '/([sducoxXbgGeEfF])$/u';
		$replacements = [];

		foreach ($matches[1] as $match) {

			list($name, $options) = array_pad(explode(':', $match, 2), 2, '');
			if(!preg_match($type_specifiers, $options))
				$options .= 's';

			$position = array_key_exists($name, $positions)
			? $positions[$name]+1 : $positions['__default__']+1;

			$pattern = '%'.$position.'$'.$options;
			$replacements['{'.$match.'}'] = $pattern;
		}

		$format = str_replace(array_keys($replacements), array_values($replacements), $format);

		return sprintf($format, ...array_values($placeholders));
	}
public function _format($format, $placeholders = [], $default = '')
	{
		$placeholders = (array) $placeholders;
		$matches = [];
		if(!preg_match_all('/\{([^{}]*)\}+/u', $format, $matches))
			return sprintf($format, ...array_values($placeholders));

		ksort($placeholders, SORT_NATURAL);
		$placeholders['__default__'] = $default;
		$positions = array_flip(array_keys($placeholders));

		$type_specifiers = '/([sducoxXbgGeEfF])$/u';
		$replacements = [];

		foreach ($matches[1] as $match) {

			list($name, $options) = array_pad(explode(':', $match, 2), 2, '');
			if(!preg_match($type_specifiers, $options))
				$options .= 's';

			$position = array_key_exists($name, $positions)
			? $positions[$name]+1 : $positions['__default__']+1;

			$pattern = '%'.$position.'$'.$options;
			$replacements['{'.$match.'}'] = $pattern;
		}

		$format = str_replace(array_keys($replacements), array_values($replacements), $format);

		return sprintf($format, ...array_values($placeholders));
	}
public function _format ( $ format , $ placeholders = [ ] , $ default = '' ) { $ placeholders = ( array ) $ placeholders ; $ matches = [ ] ; if ( ! preg_match_all ( '/\{([^{}]*)\}+/u' , $ format , $ matches ) ) return sprintf ( $ format , ... array_values ( $ placeholders ) ) ; ksort ( $ placeholders , SORT_NATURAL ) ; $ placeholders [ '__default__' ] = $ default ; $ positions = array_flip ( array_keys ( $ placeholders ) ) ; $ type_specifiers = '/([sducoxXbgGeEfF])$/u' ; $ replacements = [ ] ; foreach ( $ matches [ 1 ] as $ match ) { list ( $ name , $ options ) = array_pad ( explode ( ':' , $ match , 2 ) , 2 , '' ) ; if ( ! preg_match ( $ type_specifiers , $ options ) ) $ options .= 's' ; $ position = array_key_exists ( $ name , $ positions ) ? $ positions [ $ name ] + 1 : $ positions [ '__default__' ] + 1 ; $ pattern = '%' . $ position . '$' . $ options ; $ replacements [ '{' . $ match . '}' ] = $ pattern ; } $ format = str_replace ( array_keys ( $ replacements ) , array_values ( $ replacements ) , $ format ) ; return sprintf ( $ format , ... array_values ( $ placeholders ) ) ; }
Return a formatted string.

@param  string  $format
@param  array   $placeholders
@param  mixed   $default
@return string
Return a formatted string .