TXT转HTML
- #region TXT代码转换成HTML格式
- /// <summary>
- /// TXT代码转换成HTML格式
- /// </summary>
- /// <param name="Input">等待处理的字符串</param>
- /// <returns>处理后的字符串</returns>
- public static String ToHtml(string Input)
- {
- StringBuilder sb = new StringBuilder(Input);
- sb.Replace("&", "&");
- sb.Replace("<", "<");
- sb.Replace(">", ">");
- sb.Replace("\r\n", "<br/>");
- sb.Replace("\r", "<br/>");
- sb.Replace("\n", "<br/>");
- sb.Replace("\t", " ");
- sb.Replace(" ", " ");
- return sb.ToString();
- }
- #endregion
复制代码 HTML转TXT- #region HTML代码转换成TXT格式
- /// <summary>
- /// HTML代码转换成TXT格式
- /// </summary>
- /// <param name="Input">等待处理的字符串</param>
- /// <returns>处理后的字符串</returns>
- public static String ToTxt(String Input)
- {
- StringBuilder sb = new StringBuilder(Input);
- sb.Replace(" ", " ");
- sb.Replace("<br>", "\r\n");
- sb.Replace("<br>", "\n");
- sb.Replace("<br />", "\n");
- sb.Replace("<br />", "\r\n");
- sb.Replace("<", "<");
- sb.Replace(">", ">");
- sb.Replace("&", "&");
- return sb.ToString();
- }
- #endregion
复制代码 清除HTML标记:-
- #region 清除HTML标记
- /// <summary>
- /// 清除HTML标记
- /// </summary>
- /// <param name="Htmlstring">要清除的字符串</param>
- /// <returns></returns>
- public static string DropHTML(string Htmlstring)
- {
- //删除脚本
- Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- //删除HTML
- Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- Htmlstring.Replace("<", "");
- Htmlstring.Replace(">", "");
- Htmlstring.Replace("\r\n", "");
- Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
- return Htmlstring;
- }
- #endregion
复制代码 |