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

  • 2
    Do you want to do this programmatically. If yes, have a look at this link: stackoverflow.com/questions/2959962/… – BetaRide Jun 25 '12 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? – Marius Ilie Jun 25 '12 at 14:20
  • The answer is to short. It got converted to comment automatically :-(. You can still upvote the comment ;-). – BetaRide Jun 25 '12 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 :) – Clive Jun 25 '12 at 14:26

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.

| improve this answer | |
  • 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. – komlenic Jan 9 '19 at 14:41

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.