普通字符串与HTML相互转换的小经验

    做Web应用时经常会遇到需要用户输入文本,并且留待以后可以编辑的情况。这时候我们就不得不考虑用户输入的文本中是不是会包含HTML中的关键字的问题。
    去除HTML关键字我的办法是替换成转义字符,然后需要编辑时再把转义字符转换回来。这个步骤直接用Replace就可以,不再多说。
    这里主要是讲自动识别URL的问题,比如用户输入:
www.google.com 时自动加上超级链接标签,需要编辑时再把标签去掉。
    关于这个问题我的解决办法是使用正则表达式匹配,然后截取网址用于插入超级链接标签,转换回来时也用正则表达式匹配,然后去除标签。
    我刚开始用于匹配网址的表达式是:(
http://|www)[\S]{5 ,} 这个表达式看似可以,但是在实际应用时空个符和回车换行符会先被转为HTML转义字符串,这时候这个表达式匹配出来的结果就变成不是我们所要的了。
    后来我改成了 (
http://|www)[\S]{5,}(?:&nbsp ;) 这个表达式解决了上面所说的问题,但是测试时才发现有时候网址是写在行的结尾的或者文章结尾的,这时候就匹配不了了。并且这个表达式会捕捉到网址末尾的 这样造成生成超级链接时出错。参看了网上的正则表达式语法,明明是说(?:str)是不会捕捉str的,怎么回事呢?到现在我也没搞明白。red_smile.gif
    最后我写成 (
http://|www)[\S]{5,}(?= |\r\n ) 就OK了,(?= str)就真的不会捕捉里面的字符串。75_75.gif
    去处超级链接的正则表达式就比较简单,我的写法是:(<a href=\")[\\S]{5,}(\">)|</a>  用它匹配然后用空字符替换匹配的文本就起到去处作用了。
    具体的程序代码如下:

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
///将字符串格式化为HTML
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="normalStr">所要格式化的字符串</param>
InBlock.gif        
/// <param name="identiftyURL">是否自动识别URL,识别出来的URL将会自动加上超级链接标签</param>
ExpandedBlockEnd.gif        
/// <returns>返回格式化后的HTML代码</returns>

None.gif          public   static   string  FormatToHTML( string  normalStr,  bool  identiftyURL)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder html 
= new StringBuilder(normalStr);
InBlock.gif
InBlock.gif            html.Append(
' ');
InBlock.gif            html.Replace(
"&""&amp;");
InBlock.gif            html.Replace(
" ""&nbsp;");
InBlock.gif            html.Replace(
"<""&lt;");
InBlock.gif            html.Replace(
">""&gt;");
InBlock.gif            html.Replace(
"\"""&quot;");
InBlock.gif

InBlock.gif            
if (identiftyURL)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Regex linkRegex 
= new Regex("(http://|www)[\\S]{5,}(?=&nbsp;|\r\n)");
InBlock.gif
InBlock.gif                MatchCollection regMathes 
= linkRegex.Matches(html.ToString());
InBlock.gif
InBlock.gif                
int add = 0;
InBlock.gif
InBlock.gif                
foreach (Match match in regMathes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
string head = string.Format("<a href=\"dot.gif{0}\">", match.Value[0== 'h' ? match.Value : "http://" + match.Value);
InBlock.gif
InBlock.gif                    html.Insert(match.Index 
+ add, head);
InBlock.gif                    add 
+= head.Length;
InBlock.gif
InBlock.gif                    html.Insert(match.Index 
+ match.Length + add, "</a>");
InBlock.gif                    add 
+= 3;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            html.Replace(
"\r\n""<br />");
InBlock.gif            
InBlock.gif            
return html.ToString();
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 将HTML转为普通文本格式
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="htmlStr">所要转换的HTML字符串</param>
InBlock.gif        
/// <param name="identiftyURL">是否自动识别URL,自动识别URL会自动去处HTML代码中的超级链接标签</param>
ExpandedBlockEnd.gif        
/// <returns>返回普通文本</returns>

None.gif          public   static   string  UnFormatHTML( string  htmlStr,  bool  identiftyURL)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder normalStr 
= new StringBuilder(htmlStr);
InBlock.gif
InBlock.gif            normalStr.Replace(
"<br />""\r\n");
InBlock.gif
InBlock.gif            
if (identiftyURL)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                Regex linkRegex1 
= new Regex("(<a href=\")[\\S]dot.gif{5,}(\">)|</a>");
InBlock.gif
InBlock.gif                normalStr
= new StringBuilder(linkRegex1.Replace(normalStr.ToString(), ""));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            normalStr.Replace(
"&quot;""\"");
InBlock.gif
            normalStr.Replace("&lt;""<");
InBlock.gif            normalStr.Replace(
"&gt;"">");
InBlock.gif            normalStr.Replace(
"&nbsp;"" ");
InBlock.gif            normalStr.Replace(
"&amp;""&");
InBlock.gif
InBlock.gif            
return normalStr.ToString();
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值