10

I need to display a text and apply the chosen filter to it. The text is saved using the text_format field type.

So I know the original text, I know the chosen format type but I don't know how to actually filter the text.

How can I programmatically filter text? Thanks

4
  • 2
    Do you want to do this programmatically. If yes, have a look at this link: stackoverflow.com/questions/2959962/… Commented Jun 25, 2012 at 14:19
  • it was a bad day today. I used this so many times before but I just couldn't remember now. Can you add this as an answer so I can accept it? Commented Jun 25, 2012 at 14:20
  • The answer is to short. It got converted to comment automatically :-(. You can still upvote the comment ;-). Commented Jun 25, 2012 at 14:21
  • @BetaRide if you add a link to check_markup(), and also copy the summary description, you'll have a perfectly acceptable (even good) answer :) Commented Jun 25, 2012 at 14:26

2 Answers 2

10

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.

1
  • There is a difference between filter_id and format_id. check_markup() accepts a format_id, and will run all filters applied to that format. Commented Jan 9, 2019 at 14:41
4

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()).

2
  • 1
    This is a correct answer! Commented Feb 1, 2016 at 6:39
  • 1
    This may work for some filters, but only the simple ones. In general the process callback api.drupal.org/api/drupal/modules%21filter%21filter.api.php/… takes more parameters, including filter settings. There is also a prepare callback. Commented Jun 24, 2016 at 18:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.