Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Strip HTML tags from a string

From Wiki

Jump to: navigation, search

Summary: Strip HTML Tags from a string

If you have a string containing HTML and you need to strip all of the HTML out of it, one of the easiest methods is to use a Regular Expression.

Here's a simple example that shows how to use this expression:

  1. Imports System.Text.RegularExpressions
  2. Partial Class Default1
  3.     Inherits System.Web.UI.Page
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.         Dim myHTML As String = "<html><head></head><body style=""color:blue;"">this is some text</body></html>"
  7.         Response.Write(ConvertHtmlToPlainText(myHTML))
  8.     End Sub
  9.  
  10.     Private Function ConvertHtmlToPlainText(ByVal htmlText As String) As String
  11.         Return Regex.Replace(htmlText, "<[^>]*>", String.Empty)
  12.     End Function
  13. End Class

Ouptut:

  1. this is some text


This Hack is part of the ASP.NET Hacks collection

481 Rating: 2.9/5 (9 votes cast)