Drupal - How programmatically filter text?

This post on stackoverflow describes how to do this programmatically.

Just call check_markup and pass in your text and the filter id.

check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE)

Run all the enabled filters on a piece of text.


With check_markup() function you are forced to use text format (text format = banch of filters).

If you want to use only ONE, exact filter without full filter format (like: Convert line breaks into HTML) then use this custom function.

/**
 * Custom function to use only ONE filter wightout full format.
 *
 * @param {string} $string String to filter.
 * @param {string} $filter Filter name to use on $string
 * @return {string}        Filtered string.
 */
function filter($string, $filter) {
  $filters = filter_get_filters();
  $filter_autop = $filters[$filter];

  return $filter_autop['process callback']($string);
}

You can check list of available filters with dpm(filter_get_filters()).