记录几个字符串转html的帮助类,已防忘记
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};|"|&| |<|>|€|©|®|‰|‡|†|‹|›|„|”|“|‚|’|‘|—|–|‏|‎|‍|‌| | | |˜|ˆ|Ÿ|š|Š)", "@"); 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", " ");
text = text.Replace(" ", " "); 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(" ", "\t");
text = text.Replace(" ", " "); 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的帮助类,已防忘记的更多相关文章
- step_by_step_记录一个javascript字符串处理问题
记录一个javascript字符串处理的问题 这一天下班,技术QQ群里的大神提出了一个问题,带着问题去思考. ? '---9890.999008-555555-55555555----' 对于这样的字 ...
- JSON字符串转C#实体Class类
在项目开发过程中,经常需要和不同部门或者不同的组员一起协同工作,但对方写的json返回的结果集,我们需要用,那么如何来生成对应的类代码和实体对象呢?于是参考了网上的做法,做一个简单的字符串转实体类的功 ...
- 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 ...
- Java中将字符串与unicode的相互转换工具类
unicode编码规则 unicode码对每一个字符用4位16进制数表示.具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,如果转化的16进制数的长度不足2位,则在其后补0 ...
- 实验楼Python学习记录_挑战字符串操作
自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...
- python 零散记录(三) 格式化字符串 字符串相关方法
使用 % 符号格式化字符串: """常用转换说明符:""" #%s: 按照str()方式转换 #%r: 按照repr()方式转换 #%d: ...
- leetcode-解题记录 557. 反转字符串中的单词 III
题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出 ...
- 计算机二级-C语言-程序修改题-190113记录-对指定字符串的大小写变换处理。
//给定程序中fun函数的功能是:将p所指的字符串中每个单词的最后一个字母改成大写.(这里的“单词”是指由空格隔开的字符串) //重难点:指针对数组的遍历.大小写转换的方法.第一种使用加减32 得到, ...
- python核心编程学习记录之序列(字符串元组列表)
可以在元组中放入可变类型如列表,然后修改列表来修改元组
随机推荐
- js优化 前端小白适用
注意啦,前端初学者适合看的js优化,当你看我的优化认为太low,那么恭喜,你已经脱离初学者了. 首先这边我觉得分享的还是以js为主,前端性能优化,我认为最重要的还是js,因为js是一门解释型的语言,相 ...
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- Java stream的常见用法
不讲原理,只说用法. 1,集合遍历 public class StreamTest { public static void main(String[] args) { //1 遍历 List< ...
- 【论文速读】Cong_Yao_CVPR2017_EAST_An_Efficient_and_Accurate_Scene_Text_Detector
Cong_Yao_CVPR2017_EAST_An_Efficient_and_Accurate_Scene_Text_Detector 作者和代码 非官方版tensorflow实现 非官方版kera ...
- 判断文件的编码 python
import chardet import string path1= r'C:\Users\25456\Desktop' path = path1 + r'\深度学习.txt' with open( ...
- Windows system 在python文件操作时的路径表示方法
file_path =(r'i:\vacpy\ch10\pi_digits.txt') #将文件路径存储在变量file_path中with open (file_path) as file_objec ...
- laravel application 容器app
vendor/laravel/framework/src/Illuminate/Foundation/Application.php Application是laravel的核心容器,几乎处理所有东西 ...
- 无法加载协定为“NM3.IClrService”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
<binding name="NetTcpBinding_IClrService1" receiveTimeout="00:10:00" sendTime ...
- Redis的持久化
Redis的持久化有两种方式: RDB方式(默认支持):在指定的时间间隔内将内存中的数据集快照写入磁盘 优势 整个Redis数据库将只包含一个文件,对于文件备份来说是完美的,系统出现灾难性的故障时容易 ...
- Rsync使用方法
Rsync是一款开源.快速.多功能.可实现全量及增量的本地或者远程数据同步的优秀工具.并且支持多系统平台运行.Rsync具有本地与远程两台主机之间的数据快速复制同步镜像.远程备份等功能,该功能类似sc ...