Welcome to TechNet Blogs Sign in | Join | Help

Code to strip out <p> tags in empty placeholders

Once in a while the question how to strip <p> and <span> tags in empty placeholders comes up in the newsgroup.

A solution would be to implement a custom placeholder control that removes these tags or a workflow event.

The simplest solution is a workflow event to do this. This event would have to check if there is meaningful content in the control and if not ensure that an empty string is set a placeholder content.

Something like this:

public void CmsPosting_PlaceholderPropertyChanging( Object sender, PropertyChangingEventArgs e )  
{
   if (e.Target is HtmlPlaceholder)
   {
      if (IsEmpty(e.PropertyValue))
          e.PropertyValue = "";
   }
}

private bool IsEmpty(string content)
{
    //visible tags
    string tagsToKeep = "img|hr";                    
                            
    //This regular expression matches all tags listed above
    string regExpTagsToKeep = 
        "<[\\s|/]*(" + tagsToKeep + ")\\b[^>]*>";    
    Regex reTagsToKeep = new Regex(regExpTagsToKeep, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                                    
    //Check if one of the tags to keep is included and exit.    
    if (reTagsToKeep.IsMatch(content))
    {
        //Placeholder is not empty.                
        return false;
    }
                                    
    //This regular expression gets all tags in the content 
    string regExpForAllTags = "<[^>]*>";                
    Regex reAllTags = new Regex(regExpForAllTags, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                                    
    //Remove all Tags by replacing with an empty string
    content = reAllTags.Replace(content, "");
                                    
    //Remove all spaces and non-breaking spaces (&nbsp;) 
    content = content.Replace(" ","");            
    content = content.Replace("&nbsp;","");            
                                    
    if (content == "")
    {
        //All tags removed, we are left with an empty string    
        //Placeholder is empty.                    
        return true;                        
    }                                
    else                                
    {
        //After removing all tags, we still have content. 
        //Placeholder is not empty.                
        return false;                        
    }                                
}

 

Published Tuesday, July 12, 2005 9:34 PM by Stefan_Gossner
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Friday, August 31, 2007 5:03 PM by Eric

# re: Code to strip out <p> tags in empty placeholders

Hey, granted this is older code, but any help here would be appreachated.

I need to do this to over come a bug, but we are useing VB.net and so far nothing has worked. I have tried to use your code, but cannot find out what to include to make it all work.

Thank you in advance

Friday, August 31, 2007 5:32 PM by Stefan_Gossner

# re: Code to strip out <p> tags in empty placeholders

Hi Eric,

you need to create a custom placeholder control, derive from the original one and add the above code to it.

Sorry I cannot help you with VB.NET. Never used it. You either need to create the placeholder control in C# or port the above code to VB.NET.

Cheers,

Stefan

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker