html的帮助类

 /// <summary>
/// Represents a HTML helper
/// </summary>
public partial class HtmlHelper
{
#region Fields
private readonly static Regex paragraphStartRegex = new Regex("<p>", RegexOptions.IgnoreCase);
private readonly static Regex paragraphEndRegex = new Regex("</p>", RegexOptions.IgnoreCase);
//private static Regex ampRegex = new Regex("&(?!(?:#[0-9]{2,4};|[a-z0-9]+;))", RegexOptions.Compiled | RegexOptions.IgnoreCase); #endregion #region Utilities private static string EnsureOnlyAllowedHtml(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; const string allowedTags = "br,hr,b,i,u,a,div,ol,ul,li,blockquote,img,span,p,em,strong,font,pre,h1,h2,h3,h4,h5,h6,address,cite"; var m = Regex.Matches(text, "<.*?>", RegexOptions.IgnoreCase);
for (int i = m.Count - ; i >= ; i--)
{
string tag = text.Substring(m[i].Index + , m[i].Length - ).Trim().ToLower(); if (!IsValidTag(tag, allowedTags))
{
text = text.Remove(m[i].Index, m[i].Length);
}
} return text;
} private static bool IsValidTag(string tag, string tags)
{
string[] allowedTags = tags.Split(',');
if (tag.IndexOf("javascript") >= ) return false;
if (tag.IndexOf("vbscript") >= ) return false;
if (tag.IndexOf("onclick") >= ) return false; var endchars = new [] { ' ', '>', '/', '\t' }; int pos = tag.IndexOfAny(endchars, );
if (pos > ) tag = tag.Substring(, pos);
if (tag[] == '/') tag = tag.Substring(); foreach (string aTag in allowedTags)
{
if (tag == aTag) return true;
} return false;
}
#endregion #region Methods
/// <summary>
/// Formats the text
/// </summary>
/// <param name="text">Text</param>
/// <param name="stripTags">A value indicating whether to strip tags</param>
/// <param name="convertPlainTextToHtml">A value indicating whether HTML is allowed</param>
/// <param name="allowHtml">A value indicating whether HTML is allowed</param>
/// <param name="allowBBCode">A value indicating whether BBCode is allowed</param>
/// <param name="resolveLinks">A value indicating whether to resolve links</param>
/// <param name="addNoFollowTag">A value indicating whether to add "noFollow" tag</param>
/// <returns>Formatted text</returns>
public static string FormatText(string text, bool stripTags,
bool convertPlainTextToHtml, bool allowHtml,
bool allowBBCode, bool resolveLinks, bool addNoFollowTag)
{ if (String.IsNullOrEmpty(text))
return string.Empty; try
{
if (stripTags)
{
text = StripTags(text);
} text = allowHtml ? EnsureOnlyAllowedHtml(text) : HttpUtility.HtmlEncode(text); if (convertPlainTextToHtml)
{
text = ConvertPlainTextToHtml(text);
} if (allowBBCode)
{
text = BBCodeHelper.FormatText(text, true, true, true, true, true, true, true);
} if (resolveLinks)
{
text = ResolveLinksHelper.FormatText(text);
} if (addNoFollowTag)
{
//add noFollow tag. not implemented
}
}
catch (Exception exc)
{
text = string.Format("Text cannot be formatted. Error: {0}", exc.Message);
}
return text;
} /// <summary>
/// Strips tags
/// </summary>
/// <param name="text">Text</param>
/// <returns>Formatted text</returns>
public static string StripTags(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; text = Regex.Replace(text, @"(>)(\r|\n)*(<)", "><");
text = Regex.Replace(text, "(<[^>]*>)([^<]*)", "$2");
text = Regex.Replace(text, "(&#x?[0-9]{2,4};|&quot;|&amp;|&nbsp;|&lt;|&gt;|&euro;|&copy;|&reg;|&permil;|&Dagger;|&dagger;|&lsaquo;|&rsaquo;|&bdquo;|&rdquo;|&ldquo;|&sbquo;|&rsquo;|&lsquo;|&mdash;|&ndash;|&rlm;|&lrm;|&zwj;|&zwnj;|&thinsp;|&emsp;|&ensp;|&tilde;|&circ;|&Yuml;|&scaron;|&Scaron;)", "@"); return text;
} /// <summary>
/// replace anchor text (remove a tag from the following url <a href="http://example.com">Name</a> and output only the string "Name")
/// </summary>
/// <param name="text">Text</param>
/// <returns>Text</returns>
public static string ReplaceAnchorTags(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; text = Regex.Replace(text, @"<a\b[^>]+>([^<]*(?:(?!</a)<[^<]*)*)</a>", "$1", RegexOptions.IgnoreCase);
return text;
} /// <summary>
/// Converts plain text to HTML
/// </summary>
/// <param name="text">Text</param>
/// <returns>Formatted text</returns>
public static string ConvertPlainTextToHtml(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; text = text.Replace("\r\n", "<br />");
text = text.Replace("\r", "<br />");
text = text.Replace("\n", "<br />");
text = text.Replace("\t", "&nbsp;&nbsp;");
text = text.Replace(" ", "&nbsp;&nbsp;"); return text;
} /// <summary>
/// Converts HTML to plain text
/// </summary>
/// <param name="text">Text</param>
/// <param name="decode">A value indicating whether to decode text</param>
/// <param name="replaceAnchorTags">A value indicating whether to replace anchor text (remove a tag from the following url <a href="http://example.com">Name</a> and output only the string "Name")</param>
/// <returns>Formatted text</returns>
public static string ConvertHtmlToPlainText(string text,
bool decode = false, bool replaceAnchorTags = false)
{
if (String.IsNullOrEmpty(text))
return string.Empty; if (decode)
text = HttpUtility.HtmlDecode(text); text = text.Replace("<br>", "\n");
text = text.Replace("<br >", "\n");
text = text.Replace("<br />", "\n");
text = text.Replace("&nbsp;&nbsp;", "\t");
text = text.Replace("&nbsp;&nbsp;", " "); if (replaceAnchorTags)
text = ReplaceAnchorTags(text); return text;
} /// <summary>
/// Converts text to paragraph
/// </summary>
/// <param name="text">Text</param>
/// <returns>Formatted text</returns>
public static string ConvertPlainTextToParagraph(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; text = paragraphStartRegex.Replace(text, string.Empty);//把c#中的换行转换成html中的换行,源字符串中如果存在标签<p></p>,那么先转成c#中的换行符,最后把所有的c#换行符转换成<p></p>标签的字符串
text = paragraphEndRegex.Replace(text, "\n");
text = text.Replace("\r\n", "\n").Replace("\r", "\n");
text = text + "\n\n";
text = text.Replace("\n\n", "\n");
var strArray = text.Split(new [] { '\n' });
var builder = new StringBuilder();
foreach (string str in strArray)
{
if ((str != null) && (str.Trim().Length > ))
{
builder.AppendFormat("<p>{0}</p>\n", str);
}
}
return builder.ToString();
}
#endregion
}

锚标签的帮助类

 /// <summary>
/// Represents a ResolveLinks helper
/// </summary>
public partial class ResolveLinksHelper
{
#region Fields
/// <summary>
/// The regular expression used to parse links.
/// </summary>
private static readonly Regex regex = new Regex("((http://|https://|www\\.)([A-Z0-9.\\-]{1,})\\.[0-9A-Z?;~&\\(\\)#,=\\-_\\./\\+]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const string link = "<a href=\"{0}{1}\" rel=\"nofollow\">{2}</a>";
private const int MAX_LENGTH = ;
#endregion #region Utilities /// <summary>
/// Shortens any absolute URL to a specified maximum length
/// </summary>
private static string ShortenUrl(string url, int max)
{
if (url.Length <= max)
return url; // Remove the protocal
int startIndex = url.IndexOf("://");
if (startIndex > -)
url = url.Substring(startIndex + ); if (url.Length <= max)
return url; // Compress folder structure
int firstIndex = url.IndexOf("/") + ;
int lastIndex = url.LastIndexOf("/");
if (firstIndex < lastIndex)
{
url = url.Remove(firstIndex, lastIndex - firstIndex);
url = url.Insert(firstIndex, "...");
} if (url.Length <= max)
return url; // Remove URL parameters
int queryIndex = url.IndexOf("?");
if (queryIndex > -)
url = url.Substring(, queryIndex); if (url.Length <= max)
return url; // Remove URL fragment
int fragmentIndex = url.IndexOf("#");
if (fragmentIndex > -)
url = url.Substring(, fragmentIndex); if (url.Length <= max)
return url; // Compress page
firstIndex = url.LastIndexOf("/") + ;
lastIndex = url.LastIndexOf(".");
if (lastIndex - firstIndex > )
{
string page = url.Substring(firstIndex, lastIndex - firstIndex);
int length = url.Length - max + ;
if (page.Length > length)
url = url.Replace(page, "..." + page.Substring(length));
} return url;
}
#endregion #region Methods
/// <summary>
/// Formats the text
/// </summary>
/// <param name="text">Text</param>
/// <returns>Formatted text</returns>
public static string FormatText(string text)
{
if (String.IsNullOrEmpty(text))
return string.Empty; var info = CultureInfo.InvariantCulture;
foreach (Match match in regex.Matches(text))
{
if (!match.Value.Contains("://"))
{
text = text.Replace(match.Value, string.Format(info, link, "http://", match.Value, ShortenUrl(match.Value, MAX_LENGTH)));
}
else
{
text = text.Replace(match.Value, string.Format(info, link, string.Empty, match.Value, ShortenUrl(match.Value, MAX_LENGTH)));
}
} return text;
}
#endregion
}

bbcode的帮助类

 /// <summary>
/// Represents a BBCode helper
/// </summary>
public partial class BBCodeHelper
{
#region Fields
private static readonly Regex regexBold = new Regex(@"\[b\](.+?)\[/b\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexItalic = new Regex(@"\[i\](.+?)\[/i\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexUnderLine = new Regex(@"\[u\](.+?)\[/u\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexUrl1 = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexUrl2 = new Regex(@"\[url\](.+?)\[/url\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexQuote = new Regex(@"\[quote=(.+?)\](.+?)\[/quote\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex regexImg = new Regex(@"\[img\](.+?)\[/img\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); #endregion #region Methods
/// <summary>
/// Formats the text
/// </summary>
/// <param name="text">Text</param>
/// <param name="replaceBold">A value indicating whether to replace Bold</param>
/// <param name="replaceItalic">A value indicating whether to replace Italic</param>
/// <param name="replaceUnderline">A value indicating whether to replace Underline</param>
/// <param name="replaceUrl">A value indicating whether to replace URL</param>
/// <param name="replaceCode">A value indicating whether to replace Code</param>
/// <param name="replaceQuote">A value indicating whether to replace Quote</param>
/// <param name="replaceImg">A value indicating whether to replace Img</param>
/// <returns>Formatted text</returns>
public static string FormatText(string text, bool replaceBold, bool replaceItalic,
bool replaceUnderline, bool replaceUrl, bool replaceCode, bool replaceQuote, bool replaceImg)
{
if (String.IsNullOrEmpty(text))
return string.Empty; if (replaceBold)
{
// format the bold tags: [b][/b]
// becomes: <strong></strong>
text = regexBold.Replace(text, "<strong>$1</strong>");
} if (replaceItalic)
{
// format the italic tags: [i][/i]
// becomes: <em></em>
text = regexItalic.Replace(text, "<em>$1</em>");
} if (replaceUnderline)
{
// format the underline tags: [u][/u]
// becomes: <u></u>
text = regexUnderLine.Replace(text, "<u>$1</u>");
} if (replaceUrl)
{
var newWindow = EngineContext.Current.Resolve<CommonSettings>().BbcodeEditorOpenLinksInNewWindow;
// format the url tags: [url=http://www.nopCommerce.com]my site[/url]
// becomes: <a href="http://www.nopCommerce.com">my site</a>
text = regexUrl1.Replace(text, string.Format("<a href=\"$1\" rel=\"nofollow\"{0}>$2</a>", newWindow ? " target=_blank" : "")); // format the url tags: [url]http://www.nopCommerce.com[/url]
// becomes: <a href="http://www.nopCommerce.com">http://www.nopCommerce.com</a>
text = regexUrl2.Replace(text, string.Format("<a href=\"$1\" rel=\"nofollow\"{0}>$1</a>", newWindow ? " target=_blank" : ""));
} if (replaceQuote)
{
while (regexQuote.IsMatch(text))
text = regexQuote.Replace(text, "<b>$1 wrote:</b><div class=\"quote\">$2</div>");
} if (replaceCode)
{
text = CodeFormatHelper.FormatTextSimple(text);
} if (replaceImg)
{
// format the img tags: [img]http://www.nopCommerce.com/Content/Images/Image.jpg[/img]
// becomes: <img src="http://www.nopCommerce.com/Content/Images/Image.jpg">
text = regexImg.Replace(text, "<img src=\"$1\" class=\"user-posted-image\" alt=\"\">");
}
return text;
} /// <summary>
/// Removes all quotes from string
/// </summary>
/// <param name="str">Source string</param>
/// <returns>string</returns>
public static string RemoveQuotes(string str)
{
str = Regex.Replace(str, @"\[quote=(.+?)\]", String.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"\[/quote\]", String.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return str;
} #endregion
}

类型转换

public static int StrToInt(object obj, int? defaultVal)
{
try
{
return Convert.ToInt32(obj);
}
catch
{
return defaultVal ?? ;
}
}

记录几个字符串转html的帮助类,已防忘记的更多相关文章

  1. step_by_step_记录一个javascript字符串处理问题

    记录一个javascript字符串处理的问题 这一天下班,技术QQ群里的大神提出了一个问题,带着问题去思考. ? '---9890.999008-555555-55555555----' 对于这样的字 ...

  2. JSON字符串转C#实体Class类

    在项目开发过程中,经常需要和不同部门或者不同的组员一起协同工作,但对方写的json返回的结果集,我们需要用,那么如何来生成对应的类代码和实体对象呢?于是参考了网上的做法,做一个简单的字符串转实体类的功 ...

  3. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  4. Java中将字符串与unicode的相互转换工具类

    unicode编码规则 unicode码对每一个字符用4位16进制数表示.具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,如果转化的16进制数的长度不足2位,则在其后补0 ...

  5. 实验楼Python学习记录_挑战字符串操作

    自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...

  6. python 零散记录(三) 格式化字符串 字符串相关方法

    使用 % 符号格式化字符串: """常用转换说明符:""" #%s: 按照str()方式转换 #%r: 按照repr()方式转换 #%d: ...

  7. leetcode-解题记录 557. 反转字符串中的单词 III

    题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出 ...

  8. 计算机二级-C语言-程序修改题-190113记录-对指定字符串的大小写变换处理。

    //给定程序中fun函数的功能是:将p所指的字符串中每个单词的最后一个字母改成大写.(这里的“单词”是指由空格隔开的字符串) //重难点:指针对数组的遍历.大小写转换的方法.第一种使用加减32 得到, ...

  9. python核心编程学习记录之序列(字符串元组列表)

    可以在元组中放入可变类型如列表,然后修改列表来修改元组

随机推荐

  1. Centos7下使用Ceph-deploy快速部署Ceph分布式存储-操作记录

    之前已详细介绍了Ceph分布式存储基础知识,下面简单记录下Centos7使用Ceph-deploy快速部署Ceph环境: 1)基本环境 192.168.10.220 ceph-admin(ceph-d ...

  2. git目录

    git学习网站 https://backlog.com/git-tutorial/cn/intro/intro1_1.html

  3. ORACLE中 大量数据插入表 SQL

    declare g_commit_count number; cursor cu1 is select gl_flexfields_pkg.get_description_sql(gcc.chart_ ...

  4. day14 python各种推导式详解

    推导式的套路 之前我们已经学习了最简单的列表推导式和生成器表达式.但是除此之外,其实还有字典推导式.集合推导式等等. 下面是一个以列表推导式为例的推导式详细格式,同样适用于其他推导式. variabl ...

  5. JSON.parse与JSON.stringify

    JSON:JavaScript Object Notation(JavaScript对象表示法):甚至我们就可以大致认为JSON就是Javascript的对象,只不过范围小上一些. JSON的MIME ...

  6. Asp.Net Core WebApi 和Asp.Net WebApi上传文件

    public class UpLoadController : ControllerBase { private readonly IHostingEnvironment _hostingEnviro ...

  7. js 常用代码

    //获取url中的参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "= ...

  8. 微信小程序数组对象

    xml:<block wx:for="{{post_key}}" wx:for-item="{{item}}"></block> dat ...

  9. Git 爬坑路(从小白开始入门) ——(1)

    通过git管理项目之前,需要先注册一个GitHub账号,方便在远程仓库进行项目管理. Git之项目在本地仓库的管理(从小白开始): 一.push到远程项目 1.在个人的GitHub账号中,创建一个远程 ...

  10. deno深入揭秘及未来展望

    deno node.js之父Ryan Dahl在一个月前发起了名为deno的项目,项目的初衷是打造一个基于v8引擎的安全的TypeScript运行时,同时实现HTML5的基础API.所谓的安全运行时, ...