由于用的是FCKeditor控件,所以里面文章是通过保存html来保存格式和内容,而在显示文章简介的时候
,需要只显示前100个字,所以找了一个过滤html字符的代码,给大家共享一下。
我们很多时候都要过滤内容中的HTML代码,只需使用下面的方法就可以了。
TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]+>","");至少一个
TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]*>","");任意个
其中TempContent 表示包含有html的字符串。
比如:
要清除"<span class='lframe-t-text'>今日<font color='red'>热门新闻</font></span><div>adfadf< /div>"
中的HTML字符,只需要
string s = @"<span class='lframe-t-text'>今日<font color='red'>热门新闻</font></span><div>adfadf< /div>";
s = System.Text.RegularExpressions.Regex.Replace(s,"<[^>]+>","");
就可以了。
不过自己一直这么用:
/// <summary>
/// 取出Html中的文本
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string StripHTMLToTEXT(string strHtml)
{
string[] aryRegex ={@"<%=[/w/W]*?%>", @"<script[/w/W]*?</script>", @"<style[/w/W]*?</style>", @"<[/]?[/w/W]*?>", @"([/r/n])[/s]+",
@"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);",
@"&#(/d+);", @"-->", @"<!--.*/n"};
string[] aryReplacment = { "", "", "", "", "", " ", "/xa1", "/xa2", "/xa3", "/xa9", "", "/r/n", "" };
string strStripped = strHtml;
for (int i = 0; i < aryRegex.Length; i++)
{
Regex regex = new Regex(aryRegex[i], RegexOptions.IgnoreCase);
strStripped = regex.Replace(strStripped, aryReplacment[i]);
}
strStripped.Replace("/r/n", "");
return strStripped;
}