ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

PHP Strip tags in template?

December 04, 2008 1:53pm

Subscribe [2]
  • #1 / Dec 04, 2008 1:53pm

    LostInDaJungle

    5 posts

    I’m using EE 1.6.4.  I want to use the “strip tags” function on some truncated text. Users cut and paste their blog summary, and often it does not close HTML tags and mookies up the entire page.

    I have PHP enabled, and the PHP parsing stage set to output. So, I’m thinking that the EE tag should insert the text into the PHP function… This is part of an embedded template, and when I add the PHP code the embedded template does not show at all. (View Source shows that the DIV is not even there.)

    
    
    

    The following DOES work in a template:

    echo strip_tags("<b><ul><li>I am text</li></ul></b>", '<ul><li>');

    Here is the Debug output up to the next tag:

    (0.141308) Tag: {exp:weblog:entries weblog="tas" limit="1"}
    (0.141926) Closing Tag Found
    (0.142240) Processing Tags
    (0.142450) Module Tag: Weblog/entries
    (0.142555) Including Files for Tag and Modules
    (0.216386) Beginning Final Tag Data Processing
    (0.216516) Calling Class/Method: Weblog/entries
    (0.217810) -> Class Called: Weblog
    (0.218130) -> Method Called: entries
    (0.443655) -> Data Returned
    (0.444280) Parsing Tags in Template
    (0.444468) Tag: {exp:word_limit total="30"}

    Even this simple version fails to process:

    
    
    

    Can anyone help out here? I’m sure it’s something I’m doing wrong, but for the life of me I can’t even begin to know what it might be.

  • #2 / Dec 04, 2008 2:28pm

    Pascal Kriete

    2589 posts

    It’s probably a suppressed PHP error.  EE won’t automatically add quotes to tags - and even if it did you would still run into problems when someone puts quotes into the text.  Try catching the output in a heredoc block:

    <?php
    $summary = <<<EOT
    {summary}
    EOT;
    
    echo $summary;
    ?>
  • #3 / Dec 04, 2008 2:56pm

    LostInDaJungle

    5 posts

    That looks to be it. Thanks.

    I’m thinking that I’m going to turn this into a plugin that I can just use and bypass the output buffering. I want the option to keep certain tags and then go through and close any that might be open.

    I’m new at this, but here’s my start…

    <?php
    #Info for control panel
    $plugin_info = array(
      'pi_name' => 'Close Tags',
      'pi_version' =>'1.0',
      'pi_author' =>'Jason Maggard  ',
      'pi_author_url' => 'http://www.nothing4sale.org/',
      'pi_description' => 'Will automagically close any HTML tags left open',
      'pi_usage' => Close_tags::usage()
      );
    # Class name - This will set the tag name in EE
    class Close_tags {
        # Clear the return data var
        var $return_data = "";
        # Default function same as class name
        function Close_tags() {
            # Set template variables to global so we can access them
            global $TMPL;
            
            # And here we send the data back to the template as return data
            $this->return_data = Close_tags::closetags("$TMPL->tagdata");
    
        }
        # Here is our closetags function
        function closetags ( $html ){
            # Again, Globalize the template variables so we can get to them
            global $TMPL;
            # Get provided params
            $keep = $TMPL->fetch_param("keep_only");
            # act on them
            if ($keep) {
                $html = strip_tags($html, $keep);
            }
            # Now, we match all OPEN tags and store them
            preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
            $openedtags = $result[1];
            # Match all CLOSE tags
            preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
            $closedtags = $result[1];
            $len_opened = count ( $openedtags );
            # If we have tyhe same number of opening tags as closed tags, skip the rest
            if( count ( $closedtags ) == $len_opened ) {
                return $html;
            }
            # Reverse the array of open tags
            $openedtags = array_reverse ( $openedtags );
            # Do we have a close tag for each of our open tags
            for( $i = 0; $i < $len_opened; $i++ ) {
                if ( !in_array ( $openedtags[$i], $closedtags ) ) {
                    # No? Create one
                    $html .= "</" . $openedtags[$i] . ">";
                } else {
                    # Yes? Then remove it so we don't match it again.
                    unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
                }
            }
        return $html;
        }
      #----------------------------------------
      #  Plugin Usage
      #----------------------------------------
    
      # This function describes how the plugin is used.
      # Make sure and use output buffering
    
        function usage() {
            #Buffer start
            ob_start(); 
        ?>
    Close tags plugin simply allows you to close all tags for any text.
    
    {exp:close_tags}string{/exp:close_tags}
    
    You can also strip tags by using the "keep_only" param
    {exp:close_tags keep_only="<b>"}<b><i>string</i>{/exp:close_tags}
    
    Prints: <b>string</b>
    
      <?php
      # Buffer end - Slurp it up!
      $buffer = ob_get_contents();
        
      ob_end_clean(); 
    
      return $buffer;
      }
    
    }
    ?>

    Any feedback is welcome.

  • #4 / Dec 04, 2008 5:13pm

    Ingmar

    29245 posts

    LostInDaJungle, have your Technical Support questions been answered? If you are seeking feedback on your plugin, perhaps the plugin forums are a better place?

  • #5 / Dec 04, 2008 5:17pm

    LostInDaJungle

    5 posts

    Yes Ingmar, that answered my question. I more put that in there for anyone who might search for the same problem. I always like to post my solutions.

    Thanks a bunch for the quick help and attention to detail.

  • #6 / Dec 04, 2008 5:19pm

    Ingmar

    29245 posts

    Yes, it’s definitely good practice, and appreciated. Just wanted to followup to make sure you were all set 😊 Don’t hesitate to post again as needed.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases