2009-08-28 47 views
6

我已经安装了fckeditor,并且从MS Word粘贴时会添加大量不必要的格式。我想保留一些像大胆,斜体,公牛等等的东西。我已经搜索了网络,并提出了解决方案,即使是我想保留的东西,如大胆和斜体的东西。有没有办法剥离不必要的文字格式?有没有一种方法可以从FCKEditor中去除所有不必要的MS Word格式化

+0

任何谁的曾经保持了CMS知道哪些你所说的罪恶。祝你好运找到答案。我们只是让他们从单词粘贴,然后我有一个程序,从数据库中删除不可显示的字符。 – Steve 2009-08-29 16:38:26

回答

7

下面是我用它来擦洗从传入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 <br /> 
    '' 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 = "</?(?(?=" & AcceptableTags & ")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:([""']?).*?\1?)?)*\s*/?>" 
     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*<br[^>]*>\s*", "<br />", RegExOptions.Compiled) 
    End If 
    '' No CRs 
    html = html.Replace(controlChars.CR, "") 
    '' Convert remaining LFs to line breaks 
    html = html.Replace(controlChars.LF, "<br />") 
    '' Trim BRs at the end of any string, and spaces on either side 
    Return RegEx.Replace(html, "(<br />)+$", "", 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之后的清理都会导致不可用的编辑器(因为更改标记会更改文本光标位置)。

+0

哇......这真棒。但我确实需要链接和基本的html标签 – user161433 2009-08-29 00:59:47

0

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

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

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

Paste from Word cleanup with autodetection列为功能。

+0

pavium,fckeditor是一个RICH TEXT编辑器,它将所有使用可编辑DIV的漂亮文摘都摘录出来,并添加漂亮的工具栏。在引擎盖下,它存储在HTML中,这意味着当有人从Word中粘贴时,Word将它传递给各种各样的HTML邪恶。 – richardtallent 2009-08-28 23:39:59

2

我很理解这个问题。当复制出MS-Word(或任何文字处理或富文本编辑的文本区域)然后粘贴到FCKEditor中时(TinyMCE也会出现同样的问题),原始标记将包含在剪贴板中的内容中并进行处理。这个标记并不总是与它嵌入到粘贴操作目标中的标记互补。

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

  • 粘贴

    • 复制到记事本
    • 从记事本中选择所有
    • 复制
    • 粘贴到FCKEDITOR
  • 8

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

    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 <br /> 
         // 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 = "</?(?(?=" + AcceptableTags + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:([""']?).*?\1?)?)*\s*/?>"; 
          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*<br[^>]*>\s*", "<br />", RegexOptions.Compiled); 
         } 
    
    
         // No CRs 
         html = html.Replace("\r", ""); 
         // Convert remaining LFs to line breaks 
         html = html.Replace("\n", "<br />"); 
         // Trim BRs at the end of any string, and spaces on either side 
         return Regex.Replace(html, "(<br />)+$", "", RegexOptions.Compiled).Trim(); 
        } 
    
    3

    尝试接受的解决方案,但它没有清理字生成的标签。

    this code工作对我来说

    静态字符串CleanWordHtml(字符串HTML){

    StringCollection sc = new StringCollection(); 
    // get rid of unnecessary tag spans (comments and title) 
    sc.Add(@"<!--(\w|\W)+?-->"); 
    sc.Add(@"<title>(\w|\W)+?</title>"); 
    // 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(@"(<[^>]+>)+&nbsp;(</\w+>)+"); 
    // remove bizarre v: element attached to <img> 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; 
    } 
    
    相关问题