Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove HTML formatting in Razor MVC 3

I am using MVC 3 and Razor View engine.

What I am trying to do

I am making a blog using MVC 3, I want to remove all HTML formatting tags like <p> <b> <i> etc..

For which I am using the following code. (it does work)

 @{
 post.PostContent = post.PostContent.Replace("<p>", " ");   
 post.PostContent = post.PostContent.Replace("</p>", " ");
 post.PostContent = post.PostContent.Replace("<b>", " ");
 post.PostContent = post.PostContent.Replace("</b>", " ");
 post.PostContent = post.PostContent.Replace("<i>", " ");
 post.PostContent = post.PostContent.Replace("</i>", " ");
 }

I feel that there definitely has to be a better way to do this. Can anyone please guide me on this.

like image 997
Yasser Shaikh Avatar asked Jul 31 '12 07:07

Yasser Shaikh


2 Answers

Thanks Alex Yaroshevich,

Here is what I use now..

post.PostContent = Regex.Replace(post.PostContent, @"<[^>]*>", String.Empty);
like image 93
Yasser Shaikh Avatar answered Nov 23 '22 17:11

Yasser Shaikh


The regular expression is slow. use this, it's faster:

public static string StripHtmlTagByCharArray(string htmlString)
{
    char[] array = new char[htmlString.Length];
    int arrayIndex = 0;
    bool inside = false;

    for (int i = 0; i < htmlString.Length; i++)
    {
        char let = htmlString[i];
        if (let == '<')
        {
            inside = true;
            continue;
        }
        if (let == '>')
        {
            inside = false;
            continue;
        }
        if (!inside)
        {
            array[arrayIndex] = let;
            arrayIndex++;
        }
    }
    return new string(array, 0, arrayIndex);
}

You can take a look at http://www.dotnetperls.com/remove-html-tags

like image 29
Edi Wang Avatar answered Nov 23 '22 18:11

Edi Wang