Share via


Remove HTML tags from rich texbox

Question

Wednesday, July 13, 2016 2:47 AM

I'm using the CKEditor rich textbox control in a web application to allow users to enter multiple lines of text.  I would like remove the HTML tags from the entered text before saving the information to the database.  Does anyone know of a way to remove HTML tags from a string before saving to the database?

Ex.

Original string
<P> This is a test</P>

Desired string
This is a test

All replies (2)

Wednesday, July 13, 2016 6:20 AM

Hello Eric V! I am not sure why you want to do this. Basically the html tags are used to enhance the appearance of the text or others. If you really try to remove the tags, then use the following:

string newString = Regex.Replace(txtValue.Text, @"<.+?>| " , "" ).Trim();

The above is a regular expression that matches the html tags. Basically, the html tags <> are referred as &lt.


Wednesday, July 13, 2016 6:21 AM

Hi ,

You could refer to below article for how to use regex to remove HTML Tags :

http://www.dotnetperls.com/remove-html-tags 

Another way is to use HTML Agility Pack , you could refer to this thread for demo :

http://stackoverflow.com/questions/12787449/html-agility-pack-removing-unwanted-tags-without-removing-content 

Best Regards,

Nan Yu