8.2.x filter.module check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = array())
8.0.x filter.module check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = array())
8.1.x filter.module check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = array())
4.7.x filter.module check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE)
5.x filter.module check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE)
6.x filter.module check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE)
7.x filter.module check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE)

Runs all the enabled filters on a piece of text.

Note: Because filters can inject JavaScript or execute PHP code, security is vital here. When a user supplies a text format, you should validate it using filter_access() before accepting/using it. This is normally done in the validation stage of the Form API. You should for example never make a preview of content in a disallowed format.

Parameters

$text: The text to be filtered.

$format_id: (optional) The machine name of the filter format to be used to filter the text. Defaults to the fallback format. See filter_fallback_format().

$langcode: (optional) The language code of the text to be filtered, e.g. 'en' for English. This allows filters to be language aware so language specific text replacement can be implemented. Defaults to an empty string.

$cache: (optional) A Boolean indicating whether to cache the filtered output in the {cache_filter} table. The caller may set this to FALSE when the output is already cached elsewhere to avoid duplicate cache lookups and storage. Defaults to FALSE.

Return value

The filtered text.

Related topics

15 calls to check_markup()
BlockTestCase::testCustomBlock in modules/block/block.test
Test creating custom block, moving it to a specific region and then deleting it.
block_block_view in modules/block/block.module
Implements hook_block_view().
BookTestCase::checkBookNode in modules/book/book.test
Checks the outline of sub-pages; previous, up, and next.
BookTestCase::testBookExport in modules/book/book.test
Tests book export ("printer-friendly version") functionality.
comment_submit in modules/comment/comment.module
Prepare a comment for submission.

... See full list

File

modules/filter/filter.module, line 766
Framework for handling the filtering of content.

Code

function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE) {
  if (!isset($format_id)) {
    $format_id = filter_fallback_format();
  }
  // If the requested text format does not exist, the text cannot be filtered.
  if (!$format = filter_format_load($format_id)) {
    watchdog('filter', 'Missing text format: %format.', array('%format' => $format_id), WATCHDOG_ALERT);
    return '';
  }

  // Check for a cached version of this piece of text.
  $cache = $cache && !empty($format->cache);
  $cache_id = '';
  if ($cache) {
    $cache_id = $format->format . ':' . $langcode . ':' . hash('sha256', $text);
    if ($cached = cache_get($cache_id, 'cache_filter')) {
      return $cached->data;
    }
  }

  // Convert all Windows and Mac newlines to a single newline, so filters only
  // need to deal with one possibility.
  $text = str_replace(array("\r\n", "\r"), "\n", $text);

  // Get a complete list of filters, ordered properly.
  $filters = filter_list_format($format->format);
  $filter_info = filter_get_filters();

  // Give filters the chance to escape HTML-like data such as code or formulas.
  foreach ($filters as $name => $filter) {
    if ($filter->status && isset($filter_info[$name]['prepare callback']) && function_exists($filter_info[$name]['prepare callback'])) {
      $function = $filter_info[$name]['prepare callback'];
      $text = $function($text, $filter, $format, $langcode, $cache, $cache_id);
    }
  }

  // Perform filtering.
  foreach ($filters as $name => $filter) {
    if ($filter->status && isset($filter_info[$name]['process callback']) && function_exists($filter_info[$name]['process callback'])) {
      $function = $filter_info[$name]['process callback'];
      $text = $function($text, $filter, $format, $langcode, $cache, $cache_id);
    }
  }

  // Cache the filtered text. This cache is infinitely valid. It becomes
  // obsolete when $text changes (which leads to a new $cache_id). It is
  // automatically flushed when the text format is updated.
  // @see filter_format_save()
  if ($cache) {
    cache_set($cache_id, $text, 'cache_filter');
  }

  return $text;
}

Comments

saysilence’s picture

For anyone wondering what to put in Format_id. As mentioned somewhere else the format_id is actually a machine name of the said format defined in admin/configuration/text formats

by default there are 3 formats defined (you can use those in format_id):

'filtered_html' 
'plain_text' 
'full_html'

TheSambasti’s picture

That helped a lot, thanks!

gstout’s picture

As far as I know this is incorrect, or at least it was for me.

All my D7 sites are upgrades.

In those the format IDs are numbers for instance the machine name for 'Full HTML' is 2

hence this fails and returns nothing
check_markup($vars['fields']['title_field']->content, 'full_html', '', FALSE);

while this works
check_markup($vars['fields']['title_field']->content, '2', '', FALSE);

This could be an "I upgraded" only issue but I'd love some confirmation"

vladkras’s picture

in my case only 'full_html' works, 2 returns nothing

Vali Hutchison’s picture

full_html version worked for me too on Drupal 7.32

kingandy’s picture

It's an "I upgraded" issue. Drupal 6 used numeric IDs instead of machine names; these are preserved when upgrading because otherwise you'd have to update any text field saved referencing those IDs.

Technically the machine names can be anything, those listed above are just the ones that get created when Drupal is first installed. The only 100% reliable way to check the ones in use on your system is to go to your site's format management page (admin/config/content/formats in D7).

kingandy’s picture

(Technically you shouldn't hardcode filter formats into module or theme code but I know sometimes it's unavoidable, and it's acceptable for in-house or custom code that you won't be sharing.)