Wednesday, 21 May 2014

Strip Html Editor value to simple text using Regex

Strip Html Editor value to simple text using Regex


Introduction

Here we will strip value of html editor to simple text using Regex.

Description

We used asp.net MVC application to strip value of Html Editor to simple text.

Solution

Step 1 :  Create your Asp.net MVC 4 application first using Visual Studio 2012 or Above .

Step 2 :  Now add below code to your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;


/// <summary>
/// Strip HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Strip(string text)
{
            if (string.IsNullOrWhiteSpace(text))
                return string.Empty;

            text = HttpUtility.HtmlDecode(text);

            //replace html tags            
            text = Regex.Replace(text, @"<(.|\n)*?>", " ");

            //replace &nbsp;
            text = Regex.Replace(text, @"&nbsp;", " ");

            //replace spaces
            return Regex.Replace(text.Trim(), @"\s+", " ");
}

Step 3 :  Now access this function from your code and pass the html tags string and get the simple string value.

string result =  ClassName.Strip(htmlString);

No comments:

Post a Comment

Thank you for your interest .