format_string

7 bootstrap.inc format_string($string, array $args = array())
8 bootstrap.inc format_string($string, array $args = array())

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

7 calls to format_string()

1 string reference to 'format_string'

File

includes/bootstrap.inc, line 1533
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 '!':
        // Pass-through.
    }
  }
  return strtr($string, $args);
}

Comments

example??

Why not use an example?

Example how to use this to

Example how to use this to sanitize data from $_GET (in my case exposed filter values).

$dates = array('@min' => $_GET['date']['min']['date'], '@max' => $_GET['date']['min']['date']);
print format_string('Selected date range: @min to @max', $dates);

added in 7.8

For the record: it was introduced in version 7.8: see Change notice

Login or register to post comments