有没有办法从FCKEditor中删除所有不必要的MS Word格式

我安装了fckeditor,当从MS Word粘贴时,它添加了很多不必要的格式。 我想保留一些像粗体,斜体,公牛等等的东西。 我已经在网上搜索并提出了一些解决方案,即使是我希望保留的大胆和斜体,也能解决所有问题。 有没有办法去除不必要的单词格式?

这是我用来从富文本编辑器中擦除传入HTML的解决方案…它是用VB.NET编写的,我没有时间转换为C#,但它非常简单:

Public Shared Function CleanHtml(ByVal html As String) As String '' Cleans all manner of evils from the rich text editors in IE, Firefox, Word, and Excel '' Only returns acceptable HTML, and converts line breaks to 
'' Acceptable HTML includes HTML-encoded entities. html = html.Replace("&" & "nbsp;", " ").Trim() ' concat here due to SO formatting '' Does this have HTML tags? If html.IndexOf("<") >= 0 Then '' Make all tags lowercase html = RegEx.Replace(html, "<[^>]+>", AddressOf LowerTag) '' Filter out anything except allowed tags '' Problem: this strips attributes, including href from a '' http://stackoverflow.com/questions/307013/how-do-i-filter-all-html-tags-except-a-certain-whitelist Dim AcceptableTags As String = "i|b|u|sup|sub|ol|ul|li|br|h2|h3|h4|h5|span|div|p|a|img|blockquote" Dim WhiteListPattern As String = "" html = Regex.Replace(html, WhiteListPattern, "", RegExOptions.Compiled) '' Make all BR/br tags look the same, and trim them of whitespace before/after html = RegEx.Replace(html, "\s*]*>\s*", "
", RegExOptions.Compiled) End If '' No CRs html = html.Replace(controlChars.CR, "") '' Convert remaining LFs to line breaks html = html.Replace(controlChars.LF, "
") '' Trim BRs at the end of any string, and spaces on either side Return RegEx.Replace(html, "(
)+$", "", RegExOptions.Compiled).Trim() End Function Public Shared Function LowerTag(m As Match) As String Return m.ToString().ToLower() End Function

在您的情况下,您将要修改“AcceptableTags”中的“已批准”HTML标记列表 – 代码仍将删除所有无用的属性(不幸的是,有用的属性如HREF和SRC,希望那些不是’对你很重要)。

当然,这需要去服务器。 如果你不想这样,你需要在工具栏中添加某种“清理”按钮,调用JavaScript来搞乱编辑器的当前文本。 不幸的是,“粘贴”不是一个可以被捕获以自动清理标记的事件,并且每次OnChange之后清理会使一个不可用的编辑器(因为更改标记会改变文本光标位置)。

万一有人想要接受答案的ac#版本:

 public string CleanHtml(string html) { //Cleans all manner of evils from the rich text editors in IE, Firefox, Word, and Excel // Only returns acceptable HTML, and converts line breaks to 
// Acceptable HTML includes HTML-encoded entities. html = html.Replace("&" + "nbsp;", " ").Trim(); //concat here due to SO formatting // Does this have HTML tags? if (html.IndexOf("<") >= 0) { // Make all tags lowercase html = Regex.Replace(html, "<[^>]+>", delegate(Match m){ return m.ToString().ToLower(); }); // Filter out anything except allowed tags // Problem: this strips attributes, including href from a // http://stackoverflow.com/questions/307013/how-do-i-filter-all-html-tags-except-a-certain-whitelist string AcceptableTags = "i|b|u|sup|sub|ol|ul|li|br|h2|h3|h4|h5|span|div|p|a|img|blockquote"; string WhiteListPattern = ""; html = Regex.Replace(html, WhiteListPattern, "", RegexOptions.Compiled); // Make all BR/br tags look the same, and trim them of whitespace before/after html = Regex.Replace(html, @"\s*]*>\s*", "
", RegexOptions.Compiled); } // No CRs html = html.Replace("\r", ""); // Convert remaining LFs to line breaks html = html.Replace("\n", "
"); // Trim BRs at the end of any string, and spaces on either side return Regex.Replace(html, "(
)+$", "", RegexOptions.Compiled).Trim(); }

尝试了已接受的解决方案,但它没有清除生成标签这个词。

但是这段代码对我有用

static string CleanWordHtml(string html){

 StringCollection sc = new StringCollection(); // get rid of unnecessary tag spans (comments and title) sc.Add(@""); sc.Add(@"(\w|\W)+?"); // Get rid of classes and styles sc.Add(@"\s?class=\w+"); sc.Add(@"\s+style='[^']+'"); // Get rid of unnecessary tags sc.Add( @"<(meta|link|/?o:|/?style|/?div|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>"); // Get rid of empty paragraph tags sc.Add(@"(<[^>]+>)+ ()+"); // remove bizarre v: element attached to  tag sc.Add(@"\s+v:\w+=""[^""]+"""); // remove extra lines sc.Add(@"(\n\r){2,}"); foreach (string s in sc) { html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase); } return html; } 

我非常了解这个问题。 当复制出MS-Word(或任何文字处理或富文本编辑识别文本区域)然后粘贴到FCKEditor(TinyMCE发生同样的问题)时,原始标记包含在剪贴板中的内容中并被处理。 此标记并不总是与其粘贴到粘贴操作目标的标记互补。

除了成为FCKEditor的贡献者并研究代码并进行修改之外,我不知道解决方案。 我通常做的是指示用户执行两阶段剪贴板操作。

  • 从MS-Word复制
  • 粘贴到记事本中
  • 全选
  • 从记事本复制
  • 粘贴到FCKEDitor

但正如名称和网站建议的那样,fckeditor是一个文本编辑器 。 对我来说,这意味着它只显示文件中的字符。

没有一些额外的字符,您不能使用粗体和斜体格式。

编辑:啊,我明白了。 仔细观察Fckeditor网站,它是一个HTML编辑器,而不是我习惯的简单文本编辑器之一。

Paste from Word cleanup with autodetection作为功​​能列出。

对于我的解决方案,我结合使用了CleanHtml函数的C#版本和清除MS Office标记的部分。 基本上是Glenn流程的基于代码的版本。 我会看到当我把它推到一个巨大的Excel电子表格时会发生什么。