Improved Smarty strip_tags Modifier Plugin

I wrote this because I wanted to incorporate normal php strip_tags functionality into the Smarty modifier. I structured it so it would retain backwards compatibility with the original Smarty strip_tags modifier. You should be able to drop it into your plugins folder and run with it (name the file modifier.strip_tags.php).

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty strip_tags modifier plugin
 *
 * Type:    modifier

 * Name:    strip_tags

 * Purpose: strip html tags from text
 * @link    http://www.smarty.net/manual/en/language.modifier.strip.tags.php
 *          strip_tags (Smarty online manual)
 *
 * @author  Monte Ohrt <monte at ohrt dot com>
 * @author  Jordon Mears <jordoncm at gmail dot com>
 *
 * @version 2.0
 *
 * @param   string
 * @param   boolean optional
 * @param   string optional
 * @return  string
 */
function smarty_modifier_strip_tags($string) {
    switch(func_num_args()) {
        case 1:
            $replace_with_space = true;
            break;
        case 2:
            $arg = func_get_arg(1);
            if($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') {
                // for full legacy support || $arg === 'false' should be included
                $replace_with_space = true;
                $allowable_tags = '';
            } elseif($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
                // for full legacy support || $arg === 'false' should be removed
                $replace_with_space = false;
                $allowable_tags = '';
            } else {
                $replace_with_space = true;
                $allowable_tags = $arg;
            }
            break;
        case 3:
            $replace_with_space = func_get_arg(1);
            $allowable_tags = func_get_arg(2);
            break;
    }

    if($replace_with_space) {
        $string = preg_replace('!(<[^>]*?>)!', '$1 ', $string);
    }

    $string = strip_tags($string, $allowable_tags);

    if($replace_with_space) {
        $string = preg_replace('!(<[^>]*?>) !', '$1', $string);
    }

    return $string;
}

/* vim: set expandtab: */

?>

Two examples of same as always:

{$var|strip_tags}

{$var|strip_tags:false}

An example of php style strip_tags (will leave b and p tags):

{$var|strip_tags:'<b><p>'}

An example of both old and new functionality together:

{$var|strip_tags:false:'<b><p>'}

The original plugin (and therefore this plugin) is licensed under the LGPL.

Here is the link to its place on the Smarty plugin wiki.
http://smarty.incutio.com/?page=StripTags

Leave a Reply

{
}