C# 中 将 UBB 代码(BBC 代码)的转换成 Html 代码
2012-12-18 18:54
387 查看
直接贴代码了:
// becomes: <pre></pre>
exp = new Regex(@"\[code\](.+?)\[/code\]");
str = exp.Replace(str, "<pre>$1</pre>");
// format the code tags:
// becomes: <blockquote></blockquote>
exp = new Regex(@"\[quote\](.+?)\[/quote\]");
str = exp.Replace(str, "<blockquote>$1</blockquote>");
// format the strike tags: [s][/s]
// becomes: <strike></strike>
exp = new Regex(@"\[s\](.+?)\[/s\]");
str = exp.Replace(str, "<strike>$1</strike>");
//### Before this replace links without http ###
str.Replace("// format the url tags: [url=www.website.com" target=_blank>my site
// becomes: <a href="www.website.com">my site</a>
exp = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]");
str = exp.Replace(str, "<a rel=\"nofollow\" href=\"$1\">$2</a>");
// format the img tags:![](www.website.com/img/image.jpeg)
// becomes: <img src="www.website.com/img/image.jpeg" />
exp = new Regex(@"\[img\]([^\]]+)\[/img\]");
str = exp.Replace(str, "<img src=\"$1\" />");
// format img tags with alt: [img=www.website.com/img/image.jpeg]this is the alt text[/img]
// becomes: <img src="www.website.com/img/image.jpeg" alt="this is the alt text" />
exp = new Regex(@"\[img\=([^\]]+)\]([^\]]+)\[/img\]");
str = exp.Replace(str, "<img src=\"$1\" alt=\"$2\" />");
// format the size tags: [size=1.2][/size]
// becomes: <span style="font-size:1.2em;"></span>
exp = new Regex(@"\[size\=([^\]]+)\]([^\]]+)\[/size\]");
str = exp.Replace(str, "<span style=\"font-size:$1em;\">$2</span>");
return str;
}
}
}[/code]
谢谢浏览!
using System; using System.Text.RegularExpressions; namespace MvcUI.Extensions { public static class BBCodeUtils { /// <summary> /// 替换一个字符串中换行符(一般为“\r\n”)成特定的字符串 /// </summary> /// <param name="lines">当前字符串</param> /// <param name="replacement">要替换成的字符串</param> /// <returns></returns> public static string ReplaceLineBreaks(this string lines, string replacement) { if (string.IsNullOrEmpty(lines)) { return lines; } return lines.Replace(Environment.NewLine, replacement); } /// <summary> /// 转换基本的 BBCode 代码到 HTML /// </summary> /// <param name="str">包含 BBCode 的字符串</param> /// <returns></returns> public static string ConvertBbCodeToHtml(this string str) { // As this is a BBEditor we need to replace line breaks // or they won't show in the front end str = ReplaceLineBreaks(str, "<br>"); // format the bold tags: // becomes: <strong></strong> var exp = new Regex(@"\[b\](.+?)\[/b\]"); str = exp.Replace(str, "<strong>$1</strong>"); // format the italic tags: // becomes: <em></em> exp = new Regex(@"\[i\](.+?)\[/i\]"); str = exp.Replace(str, "<em>$1</em>"); // format the underline tags: // becomes: <u></u> exp = new Regex(@"\[u\](.+?)\[/u\]"); str = exp.Replace(str, "<u>$1</u>"); // format the underline tags: [ul][/ul] // becomes: <ul></ul> exp = new Regex(@"\[ul\](.+?)\[/ul\]"); str = exp.Replace(str, "<ul>$1</ul>"); // format the underline tags: [ol][/ol] // becomes: <ol></ol> exp = new Regex(@"\[ol\](.+?)\[/ol\]"); str = exp.Replace(str, "<ol>$1</ol>"); // format the underline tags: [li][/li] // becomes: <li></li> exp = new Regex(@"\[li\](.+?)\[/li\]"); str = exp.Replace(str, "<li>$1</li>"); // format the code tags: [code]
// becomes: <pre></pre>
exp = new Regex(@"\[code\](.+?)\[/code\]");
str = exp.Replace(str, "<pre>$1</pre>");
// format the code tags:
// becomes: <blockquote></blockquote>
exp = new Regex(@"\[quote\](.+?)\[/quote\]");
str = exp.Replace(str, "<blockquote>$1</blockquote>");
// format the strike tags: [s][/s]
// becomes: <strike></strike>
exp = new Regex(@"\[s\](.+?)\[/s\]");
str = exp.Replace(str, "<strike>$1</strike>");
//### Before this replace links without http ###
str.Replace("// format the url tags: [url=www.website.com" target=_blank>my site
// becomes: <a href="www.website.com">my site</a>
exp = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]");
str = exp.Replace(str, "<a rel=\"nofollow\" href=\"$1\">$2</a>");
// format the img tags:
![](www.website.com/img/image.jpeg)
// becomes: <img src="www.website.com/img/image.jpeg" />
exp = new Regex(@"\[img\]([^\]]+)\[/img\]");
str = exp.Replace(str, "<img src=\"$1\" />");
// format img tags with alt: [img=www.website.com/img/image.jpeg]this is the alt text[/img]
// becomes: <img src="www.website.com/img/image.jpeg" alt="this is the alt text" />
exp = new Regex(@"\[img\=([^\]]+)\]([^\]]+)\[/img\]");
str = exp.Replace(str, "<img src=\"$1\" alt=\"$2\" />");
// format the size tags: [size=1.2][/size]
// becomes: <span style="font-size:1.2em;"></span>
exp = new Regex(@"\[size\=([^\]]+)\]([^\]]+)\[/size\]");
str = exp.Replace(str, "<span style=\"font-size:$1em;\">$2</span>");
return str;
}
}
}[/code]
谢谢浏览!
相关文章推荐
- C#中HTML和UBB互相转换的代码
- C#中UBB转换为HTML的代码(转自:小逸)
- c#2005中的各个控件转换为html代码
- ThinkPHP HTML标签代码和UBB互相转换
- 将WORD文档转换成为HTML网页文件的C#代码
- C#实现HTML和UBB互相转换的方法
- C#HTML与UBB(纯文本)之间的转换
- 简单实用的HTML到UBB转换脚本工具实现说明
- C# 将Word,Excel转换成Html
- c# il (中间代码)exe,dll(编译成的程序) 之间的转换
- C#检查HTML是否闭合以及自动修复代码
- HTML源代码转换JavaScript代码工具
- C#与VB代码转换
- C#将HTML文本转换为普通文本,去掉所有的Html标记
- 5个最优秀的Java和C#代码转换工具
- c#实现16进制和字符串之间转换的代码
- 将代码转换为语法高亮的HTML
- PHP将HTML转换成文本的实现代码
- 在C#winform中运行js代码,需要自己转换提供所需要的JS
- VB.Net C#代码转换工具收藏