Wednesday, July 28, 2010

ASP Net - Stripping html tags from text

 ASP Net - Stripping html tags from text

Remove any tag from a html string.

Extract a specific tag in html using c#

Solution:-

Is pretty simple you just pass the html string and the tag name which you want  to remove or extract in this fucnction ,this will do that for you.

Like if you want to remove all occurence of  a img tag from a html string.please pass your html string and the tag name as img and your result is ready to you.


private string GetImagesInHTMLString(string htmlString,string tag)

    {       

        string pattern = @"<(" + tag + @")\b[^>]*>";



        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);

        MatchCollection matches = rgx.Matches(htmlString);



        for (int i = 0, l = matches.Count; i < l; i++)

        {

            htmlString = htmlString.Replace(matches[i].Value, "");         

        }



        return htmlString;

    }

No comments:

Post a Comment

Popular Posts