function format_string

Replaces placeholders with sanitized values in a string.

Parameters

$string: A string containing placeholders.

$args: An associative array of replacements to make. Occurrences in $string of any key in $args are replaced with the corresponding value, after sanitization. The sanitization function depends on the first character of the key:

  • !variable: Inserted as is. Use this for text that has already been sanitized.
  • @variable: Escaped to HTML using check_plain(). Use this for anything displayed on a page on the site.
  • %variable: Escaped as a placeholder for user-submitted content using drupal_placeholder(), which shows up as <em>emphasized</em> text.

See also

t()

Related topics

281 calls to format_string()
1 string reference to 'format_string'

File

drupal/core/includes/bootstrap.inc, line 1548
Functions that need to be loaded on every Drupal request.

Code

function format_string($string, array $args = array()) {

  // Transform arguments before inserting them.
  foreach ($args as $key => $value) {
    switch ($key[0]) {
      case '@':

        // Escaped only.
        $args[$key] = check_plain($value);
        break;
      case '%':
      default:

        // Escaped and placeholder.
        $args[$key] = drupal_placeholder($value);
        break;
      case '!':
    }
  }
  return strtr($string, $args);
}