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()
AddFeedTest::testBasicFeedAddNoTitle in drupal/core/modules/system/lib/Drupal/system/Tests/Common/AddFeedTest.php
Tests drupal_add_feed() with paths, URLs, and titles.
AggregatorConfigurationTest::testSettingsPage in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorConfigurationTest.php
Tests the settings form to ensure the correct default values are used.
AggregatorRenderingTest::testBlockLinks in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorRenderingTest.php
Add a feed block to the page and checks its links.
AggregatorTestBase::createFeed in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php
Create an aggregator feed (simulate form submission on admin/config/services/aggregator/add/feed).
AggregatorTestBase::updateFeedItems in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php
Update feed items (simulate click to admin/config/services/aggregator/update/$fid).

... See full list

1 string reference to 'format_string'
XssUnitTest::testFormatStringAndT in drupal/core/modules/system/lib/Drupal/system/Tests/Common/XssUnitTest.php
Tests t() and format_string() replacement functionality.

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);
}