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

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

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

         public   static   string  FormatToHTML( string  normalStr,  bool  identiftyURL)
        
{
            StringBuilder html 
= new StringBuilder(normalStr);

            html.Append(
' ');
            html.Replace(
"&""&amp;");
            html.Replace(
" ""&nbsp;");
            html.Replace(
"<""&lt;");
            html.Replace(
">""&gt;");
            html.Replace(
"/"""&quot;");

            
if (identiftyURL)
            
{
                Regex linkRegex 
= new Regex("(http://|www)[//S]{5,}(?=&nbsp;|/r/n)");

                MatchCollection regMathes 
= linkRegex.Matches(html.ToString());

                
int add = 0;

                
foreach (Match match in regMathes)
                
{
                    
string head = string.Format("<a href=/"{0}/">", match.Value[0== 'h' ? match.Value : "http://" + match.Value);

                    html.Insert(match.Index 
+ add, head);
                    add 
+= head.Length;

                    html.Insert(match.Index 
+ match.Length + add, "</a>");
                    add 
+= 3;
                }

            }


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


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

         public   static   string  UnFormatHTML( string  htmlStr,  bool  identiftyURL)
        
{
            StringBuilder normalStr 
= new StringBuilder(htmlStr);

            normalStr.Replace(
"<br />""/r/n");

            
if (identiftyURL)
            
{
                Regex linkRegex1 
= new Regex("(<a href=/")[//S]{5,}(/">)|</a>");

                normalStr
= new StringBuilder(linkRegex1.Replace(normalStr.ToString(), ""));
            }


            normalStr.Replace(
"&quot;""/"");
            normalStr.Replace("&lt;""<");
            normalStr.Replace(
"&gt;"">");
            normalStr.Replace(
"&nbsp;"" ");
            normalStr.Replace(
"&amp;""&");

            
return normalStr.ToString();
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值