wp_sprintf
函数
wp_sprintf ( $pattern, $args )
- 参数
-
-
(string)
$pattern
The string which formatted args are inserted.- Required: 是
-
(mixed)
$args
Arguments to be formatted into the $pattern string.- Required: 是
-
(string)
- 返回值
-
- (string) The formatted string.
- 定义位置
-
-
wp-includes/formatting.php
, line 5117
-
wp-includes/formatting.php
- 引入
- 2.5.0
- 弃用
- –
WordPress implementation of PHP sprintf() with filters.
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;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。