我的文本转HTML加超链接函数

以前曾经看到一个不错的文本转HTML的函数,可惜被我弄丢了。幸好还记得原理,今天决定自己写写。

在要使用的.aspx的@Page中加入 ValidateRequest="false",使用到的namespace:System.Text,System.Text.RegularExpressions。

ExpandedBlockStart.gif ContractedBlock.gif /**/     /// <summary>
InBlock.gif    
/// 将普通字符串格式化为HTML
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="textStr">所要格式化的字符串</param>
InBlock.gif    
/// <param name="spotUrlEmail">是否自动识别"http://"或"https://"开头的URL和Email地址,识别出来的URL和Email将会自动加上超链接</param>
ExpandedBlockEnd.gif    
/// <returns>格式化后的HTML代码</returns>

None.gif      public   static   string  TextToHTML( string  textStr,  bool  spotUrlEmail)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        StringBuilder html 
= new StringBuilder(textStr);
InBlock.gif
InBlock.gif        
//html.Replace("&", "&amp;");   //2006-4-26修改。不对"&"进行转义了,不然无法处理好多个QueryString的URL
InBlock.gif
        html.Replace("  "" &nbsp;");  //两个空格才转义,是为了较好处理带QueryString的URL后接空格的情况
InBlock.gif
        html.Replace("<""&lt;");
InBlock.gif        html.Replace(
">""&gt;");
InBlock.gif        html.Replace(
"\"""&quot;");
InBlock.gif
        html.Replace("\n""<br />");  //IE中的换行为"\r\n",FF中为"\n"
InBlock.gif
InBlock.gif        
if (spotUrlEmail)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int offset;
InBlock.gif
InBlock.gif            Regex linkRegex 
= new Regex("(http(s)?://)([\\w-]+\\.)+[\\w-]+(/[\\w-./?&%=]*)?");
InBlock.gif            MatchCollection linkMatches 
= linkRegex.Matches(html.ToString());
InBlock.gif            offset 
= 0;
InBlock.gif            
foreach (Match match in linkMatches)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string linkHead = string.Format("<a href=\"dot.gif{0}\">", match.Value);
InBlock.gif
InBlock.gif                html.Insert(match.Index 
+ offset, linkHead);
InBlock.gif                offset 
+= linkHead.Length;
InBlock.gif
InBlock.gif                html.Insert(match.Index 
+ match.Length + offset, "</a>");
InBlock.gif                offset 
+= 4;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Regex emailRegex 
= new Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
InBlock.gif            MatchCollection emailMatches 
= emailRegex.Matches(html.ToString());
InBlock.gif            offset 
= 0;
InBlock.gif            
foreach (Match match in emailMatches)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string emailHead = string.Format("<a href=\"mailto:dot.gif{0}\">", match.Value);
InBlock.gif
InBlock.gif                html.Insert(match.Index 
+ offset, emailHead);
InBlock.gif                offset 
+= emailHead.Length;
InBlock.gif
InBlock.gif                html.Insert(match.Index 
+ match.Length + offset, "</a>");
InBlock.gif                offset 
+= 4;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
        
InBlock.gif
InBlock.gif        
return html.ToString();
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/dxz/archive/2006/04/25/text_to_html.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值