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 ?

naanace
  • 363
  • 1
  • 4
  • 17
  • 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]: http://stackoverflow.com/questions/4878452/remove-html-tags-in-string [2]: http://stackoverflow.com/questions/785715/how-can-i-strip-html-tags-from-a-string-in-asp-net [3]: http://stackoverflow.com/questions/19523913/remove-html-tags-from-string-including-nbsp-in-c-sharp – Roman Bezrabotny Mar 20 '15 at 06:01
  • @RomanBezrabotny ohhh you find a lot references. Thanks ;) I need to read more – naanace Mar 20 '15 at 06: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 – Sami Kuhmonen Mar 20 '15 at 08:54

1 Answers1

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.

wp78de
  • 18,207
  • 7
  • 43
  • 71
Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49
  • What if I can't find `Regex`?? – naanace Mar 20 '15 at 06:06
  • \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). – Sami Kuhmonen Mar 20 '15 at 08:57