2

I used CKEditor ASP.NET version and adjust to my writing space. When click btn_Post button, then should post written text in this editor field. I want to get this text in C# because for saving at database. So I searched how to use(here) and found the way using HtmlEncode. Here is codes what I found.

asp

<div>
  <CKEditor:CKEditorControl ID="CKEditor1" BasePath="/ckeditor/" runat="server">
  </CKEditor:CKEditorControl>
</div>
<div style="margin-top:10px; float:right;">
  <asp:button ID="btn_Post" runat="server" Text="등록하기" CssClass="btn_Post" onclick="btn_Post_Click" />    
</div>

CS

string str = CKEditor1.Text;
string str1 = Server.HtmlEncode(str);
string str2 = Server.HtmlDecode(str);
//str = <p>1234</p>\r\n
//str1 = &lt;p&gt;1234&lt;/p&gt;\r\n
//str2 = <p>1234</p>\r\n 

But the problem is, I need to save text with none html codes. As you can see, all variable shows html code. How can I change this result to pure text 1234 ?

3
  • I hope this will help you [Remove HTML tags in String][1] [How can I strip HTML tags from a string in ASP.NET?][2] [Remove HTML tags from string including &nbsp in C#][3] [1]: stackoverflow.com/questions/4878452/remove-html-tags-in-string [2]: stackoverflow.com/questions/785715/… [3]: stackoverflow.com/questions/19523913/… Commented Mar 20, 2015 at 6:01
  • @RomanBezrabotny ohhh you find a lot references. Thanks ;) I need to read more Commented Mar 20, 2015 at 6:09
  • Just wondering, why do you need CKEditor if you just want to have the text? Wouldn't a simple textarea be a lot better then? Or if you just need the plain text in addition to the HTML text, then I understand Commented Mar 20, 2015 at 8:54

1 Answer 1

0

You can use this method

public static string RemoveHTMLTags(string content)
        {
            var cleaned = string.Empty;
            try
            {
                string textOnly = string.Empty;
                Regex tagRemove = new Regex(@"<[^>]*(>|$)");
                Regex compressSpaces = new Regex(@"[\s\r\n]+");
                textOnly = tagRemove.Replace(content, string.Empty);
                textOnly = compressSpaces.Replace(textOnly, " ");
                cleaned = textOnly;
            }
            catch
            {
                //A tag is probably not closed. fallback to regex string clean.

            }

            return cleaned;
        }

Or use HTML Agility Pack to remove all HTML tags.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I can't find Regex??
\s "always" already includes \r and \n, so they don't need to be included separately. ("Always" since some implementation might not include them, but all regularly used do and the regular .NET one especially does).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.