Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Modifier strip_tags

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Sun Aug 26, 2012 11:08 pm    Post subject: Modifier strip_tags Reply with quote

I created a version without using the crappy PHP function strip_tags or the used RegEx which kills everything between a < and a > for a customer of mine. Is there any place where I can provide this enchanced version to the Smarty community?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4668
Location: Hamburg / Germany

PostPosted: Mon Aug 27, 2012 4:53 pm    Post subject: Reply with quote

You post it just here.
Back to top
View user's profile Send private message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Mon Aug 27, 2012 5:30 pm    Post subject: Reply with quote

Ah okay.. my version is for Smarty 2.x. I will make it work with 3.x too and post.
Back to top
View user's profile Send private message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Wed Aug 29, 2012 10:54 pm    Post subject: Reply with quote

For Smarty 2.x
Code:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty strip_tags modifier plugin
 *
 * Type:     modifier<br>
 * Name:     strip_tags<br>
 * Purpose:  strip html tags from text
 * @link http://smarty.php.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>
 * @author  Tekin Birdüzen <t.birduezen at web-coding dot eu>
 *
 * @version 3.0
 *
 * @param string
 * @param boolean optional
 * @param string optional
 * @return string
 */
function smarty_modifier_strip_tags($string)
{
    // HTML5 selfclosing tags
    $selfclosingTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');

    /**
     * Find out how many arguments we have and
     * initialize the needed variables
     */
    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 = ' ';
                $allowed_tags = '';
            } elseif ($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
                // for full legacy support || $arg === 'false' should be removed
                $replace_with_space = '';
                $allowed_tags = '';
            } else {
                $replace_with_space = ' ';
                $allowed_tags = $arg;
            }
            break;
        case 3:
            $replace_with_space = func_get_arg(1) ? ' ' : '';
            $allowed_tags = func_get_arg(2);
            break;
    }

    if (strlen($allowed_tags)) {
        // Allowed tags are set
        $allowed_tags = str_replace(array(' />', '/>', '>'), '', $allowed_tags);
        $allowed_tags = substr(str_replace('<', '|', $allowed_tags), 1);

        // This is to delete the allowed selfclosing tags from the list
        $tagArray = explode('|', $allowed_tags);
        $selfclosingTags = array_diff($selfclosingTags, $tagArray);
        unset ($tagArray);
    }

    // Let's get rid of the selfclosing tags first
    if (count($selfclosingTags)) {
        $string = preg_replace('/<(' . implode('|', $selfclosingTags) . ')\s?[^>]*?\/>/is', $replace_with_space, $string);
    }

    // And now the other tags
    if (strlen($allowed_tags)) {
        while (preg_match("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", $string))
            $string = preg_replace("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", '$3' . $replace_with_space, $string);
    }
    else {
        // Absolutely no tags allowed
        while (preg_match("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", $string))
            $string = preg_replace("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", '$2' . $replace_with_space, $string);
    }

    return $string;
}

/* vim: set expandtab: */
?>

The 3.x version will follow soon.


Edit: Hmm.. did I understand it right? When you delete modifiercompiler.strip_tags.php and upload the above code as modifier.strip_tags.php then there is nothing to change for 3.x?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4668
Location: Hamburg / Germany

PostPosted: Wed Aug 29, 2012 11:08 pm    Post subject: Reply with quote

Normally Smarty2 modifier do run also under Smarty3.

Yes, in your case you must remove the modifiercompiler.strip_tags.php file because of the search order for modifier.
Back to top
View user's profile Send private message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Wed Aug 29, 2012 11:43 pm    Post subject: Reply with quote

btw.. the usage is the same as the version of Jordon Mears.

  • {$string|strip_tags} strips all tags and replaces them with a space
  • {$string|strip_tags:false} strips all tags without replacing them with a space
  • {$string|strip_tags:'<b><br>'} strips all tags except b and br tags and replaces them with a space
  • {$string|strip_tags:false:'<b><br>'} strips all tags except b and br tags without replacing them with a space


It doesn't matter if you write the allowed tags like <br> or <br /> or even <br/>. All of those are recognized.

I use Smarty almost in every project I do and this shall be my thank for it. Smile I hope my version will become official because using strip_tags PHP is really crappy. I learned to hate it Very Happy
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4668
Location: Hamburg / Germany

PostPosted: Thu Aug 30, 2012 12:16 am    Post subject: Reply with quote

Thanks for your input. I think we will include it in the upcomming major 3.2 release.
Back to top
View user's profile Send private message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Thu Aug 30, 2012 10:16 pm    Post subject: Reply with quote

Gern geschehen. Euch einen Dank für das wunderbare Templatesystem. Smile
Back to top
View user's profile Send private message
cYbercOsmOnauT
Smarty Rookie


Joined: 26 Aug 2012
Posts: 6
Location: Göttingen, Germany

PostPosted: Tue Apr 02, 2013 12:22 pm    Post subject: Reply with quote

There was a small problem with the selfclosing tags. The fixed version:
Code:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty strip_tags modifier plugin
 *
 * Type:     modifier<br>
 * Name:     strip_tags<br>
 * Purpose:  strip html tags from text
 * @link http://smarty.php.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>
 * @author  Tekin Birdüzen <t.birduezen at web-coding dot eu>
 *
 * @version 3.0
 *
 * @param string
 * @param boolean optional
 * @param string optional
 * @return string
 */
function smarty_modifier_strip_tags($string)
{
    // HTML5 selfclosing tags
    $selfclosingTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');

    /**
     * Find out how many arguments we have and
     * initialize the needed variables
     */
   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 = ' ';
            $allowed_tags = '';
         } elseif ($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
            // for full legacy support || $arg === 'false' should be removed
            $replace_with_space = '';
            $allowed_tags = '';
         } else {
            $replace_with_space = ' ';
            $allowed_tags = $arg;
         }
         break;
      case 3:
         $replace_with_space = func_get_arg(1) ? ' ' : '';
         $allowed_tags = func_get_arg(2);
         break;
   }
   if (strlen($allowed_tags)) {
      // Allowed tags are set
      $allowed_tags = str_replace(array(' />', '/>', '>'), '', $allowed_tags);

      // This is to delete the allowed selfclosing tags from the list
      $tagArray = explode('<', substr($allowed_tags, 1));
      $selfClosing = array_intersect($tagArray, $selfclosingTags);
      $selfclosingTags = array_diff($selfclosingTags, $tagArray);
      $allowed_tags = implode('|', array_diff($tagArray, $selfClosing));

      unset ($tagArray, $selfClosing);
   }

   // Let's get rid of the selfclosing tags first
   if (count($selfclosingTags)) {
        $string = preg_replace('/<(' . implode('|', $selfclosingTags) . ')\s?[^>]*?\/?>/is', $replace_with_space, $string);
    }

    // And now the other tags
    if (strlen($allowed_tags)) {
        while (preg_match("/<(?!({$allowed_tags}))([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\2>/is", $string))
            $string = preg_replace("/<(?!({$allowed_tags}))([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\2>/is", '$3' . $replace_with_space, $string);
    }
    else {
        // Absolutely no tags allowed
        while (preg_match("/<([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\1>/is", $string))
            $string = preg_replace("/<([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\1>/is", '$2' . $replace_with_space, $string);
    }

    return $string;
}

/* vim: set expandtab: */
?>
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP