Changeset 6974 for trunk/wp-includes/formatting.php
- Timestamp:
- 02/22/2008 05:53:47 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/formatting.php
r6767 r6974 1278 1278 } 1279 1279 1280 /** 1281 * wp_sprintf() - sprintf() with filters 1282 */ 1283 function wp_sprintf( $pattern ) { 1284 $args = func_get_args( ); 1285 $len = strlen($pattern); 1286 $start = 0; 1287 $result = ''; 1288 $arg_index = 0; 1289 while ( $len > $start ) { 1290 // Last character: append and break 1291 if ( strlen($pattern) - 1 == $start ) { 1292 $result .= substr($pattern, -1); 1293 break; 1294 } 1295 1296 // Literal %: append and continue 1297 if ( substr($pattern, $start, 2) == '%%' ) { 1298 $start += 2; 1299 $result .= '%'; 1300 continue; 1301 } 1302 1303 // Get fragment before next % 1304 $end = strpos($pattern, '%', $start + 1); 1305 if ( false === $end ) 1306 $end = $len; 1307 $fragment = substr($pattern, $start, $end - $start); 1308 1309 // Fragment has a specifier 1310 if ( $pattern{$start} == '%' ) { 1311 // Find numbered arguments or take the next one in order 1312 if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) { 1313 $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : ''; 1314 $fragment = str_replace("%{$matches[1]}$", '%', $fragment); 1315 } else { 1316 ++$arg_index; 1317 $arg = isset($args[$arg_index]) ? $args[$arg_index] : ''; 1318 } 1319 1320 // Apply filters OR sprintf 1321 $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg ); 1322 if ( $_fragment != $fragment ) 1323 $fragment = $_fragment; 1324 else 1325 $fragment = sprintf($fragment, strval($arg) ); 1326 } 1327 1328 // Append to result and move to next fragment 1329 $result .= $fragment; 1330 $start = $end; 1331 } 1332 return $result; 1333 } 1334 1335 /** 1336 * wp_sprintf_l - List specifier %l for wp_sprintf 1337 * 1338 * @param unknown_type $pattern 1339 * @param unknown_type $args 1340 * @return unknown 1341 */ 1342 function wp_sprintf_l($pattern, $args) { 1343 // Not a match 1344 if ( substr($pattern, 0, 2) != '%l' ) 1345 return $pattern; 1346 1347 // Nothing to work with 1348 if ( empty($args) ) 1349 return ''; 1350 1351 // Translate and filter the delimiter set (avoid ampersands and entities here) 1352 $l = apply_filters('wp_sprintf_l', array( 1353 'between' => _c(', |between list items'), 1354 'between_last_two' => _c(', and |between last two list items'), 1355 'between_only_two' => _c(' and |between only two list items'), 1356 )); 1357 1358 $args = (array) $args; 1359 $result = array_shift($args); 1360 if ( count($args) == 1 ) 1361 $result .= $l['between_two'] . array_shift($args); 1362 // Loop when more than two args 1363 while ( count($args) ) { 1364 $arg = array_shift($args); 1365 if ( $i == 1 ) 1366 $result .= $l['between_last_two'] . $arg; 1367 else 1368 $result .= $l['between'] . $arg; 1369 } 1370 return $result . substr($pattern, 2); 1371 } 1372 1280 1373 ?>
Note: See TracChangeset
for help on using the changeset viewer.