清理Word生成HTML的冗余;清理与清除HTML标签

清理Word生成HTML的冗余;清理与清除HTML标签

众所周知,当直接将word中的内容复制到网页上时,会产生很多冗余代码;

而现在,在线编辑器又很普遍;就包括,现在用的百度空间的这个文本编辑器,如果直接从WORD中写好的文章复制进来,本来没有几个字,结果,它会提示,超出最大字数;也就是因为冗余代码过多的原因;

而用户直接拷贝Word的事儿很常见;以前我也写过一个清理的方法;在我的空间也有;当时只是为了让文章在显示时,很够统一格式;清理了一些多余样式,然后用CSS控件其样式,如果控件不了的,就用!importan强制定义;呵呵;

虽然清理了样式,但也遗留了不少,例如,<font >示例</font>;清理后成了<font>示例></font>;其实这时font标签已经没有用了,不如直接清除字;

于是就又写了一些方法;把几个常用的简单方法发上来吧,很简单;

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

namespace Extend

{

public class Article

{

#region 清理HTML标签

/// <summary>

/// 清理HTML标签的多余样式;如<div >示例</div>;换成<div>示例</div>

/// </summary>

/// <param name="str">原始文本</param>

/// <param name="element">要清除的标签</param>

/// <returns></returns>

public static string ClearElement(string str, string element)

{

string old = @"<" + element + "[^>]+>";

string rep = "<" + element + ">";

str = Regex.Replace(str, old, rep, RegexOptions.IgnoreCase);

return str;

}

/// <summary>

/// 清除HTML标签;如<div >示例</div>;换成:示例

/// </summary>

/// <param name="str">原始文本</param>

/// <param name="element">要清除的标签</param>

/// <returns></returns>

public static string ReMoveElement(string str,string element)

{

string regFront = @"<" + element + "[^>]*>";

string regAfter = "</" + element + ">";

str = Regex.Replace(str, regFront, "", RegexOptions.IgnoreCase);

str = Regex.Replace(str, regAfter, "", RegexOptions.IgnoreCase);

return str;

}

/// <summary>

/// 清理指定字符串,大小写不敏感

/// </summary>

/// <param name="strText">原始文本</param>

/// <param name="strOld">要替换的字符串,支持正则表达式,大小写不敏感</param>

/// <param name="strNew">替换后的字符串</param>

/// <returns></returns>

public static string RegexReplace(string strText,string strOld,string strNew)

{

strText = Regex.Replace(strText, strOld, strNew, RegexOptions.IgnoreCase);

return strText;

}

/// <summary>

/// 清理Word的样式,主要是一些带冒号的标签,如o:p

/// </summary>

/// <param name="strText"></param>

/// <returns></returns>

public static string ClearWordStyle(string strText)

{

string regFront = @"<\w+:[^>]*>";

string regAfter = @"</\w+:[^>]*>";

strText = Regex.Replace(strText, regFront, "", RegexOptions.IgnoreCase);

strText = Regex.Replace(strText, regAfter, "", RegexOptions.IgnoreCase);

return strText;

}

#endregion

}

}

以上只是清理的方法;实际操作时,可以这样写;

/// <summary>

/// 替换新闻内容中的Html标签的多余属性

/// </summary>

/// <param name="str"></param>

/// <returns></returns>

private string ArtilceClear(string str)

{

if (str == "" || str == null || string.IsNullOrEmpty(str))

return "";

//清理word标签,如o:p之类,带冒号的

str = Extend.Article.ClearWordStyle(str);

string[] el;

//清理样式

el = new string[] { "p", "div","table","tr","td" };

foreach (string s in el)

{

try

{

str = Extend.Article.ClearElement(str, s);

}

catch

{

continue;

}

}

//清除样式

el = new string[] { "span", "strong", "font", "h1", "tbody","o:p" };

foreach (string s in el)

{

try

{

str = Extend.Article.ReMoveElement(str, s);

//while (str.IndexOf("</"+s+">") >-1)

//{

// str = Extend.Article.ReMoveElement(s, str);

//}

}

catch

{

continue;

}

}

str = Extend.Article.RegexReplace(str," ","");

return str;

}

注意看“清理”还是“清除”;像P、div、table等,是不能清除的,只是将它们的样式清理一下,将冗余代码去掉,该标签并不删除;而像span、font、o:p等,可以连标签清除掉;

上面的代码,只作为参考;更复杂的按条件清理,可以参看我以前的文章;一般的清理,上面的代码,也足够了;