zoukankan      html  css  js  c++  java
  • 字符串 html之间的转换

     1   /**/
     2         /// <summary>
     3         ///将字符串格式化为HTML
     4         /// </summary>
     5         /// <param name="normalStr">所要格式化的字符串</param>
     6         /// <param name="identiftyURL">是否自动识别URL,识别出来的URL将会自动加上超级链接标签</param>
     7         /// <returns>返回格式化后的HTML代码</returns>
     8         public static string FormatToHTML(string normalStr, bool identiftyURL)
     9         {
    10             StringBuilder html = new StringBuilder(normalStr);
    11 
    12             html.Append(' ');
    13             html.Replace("&amp;", "&");
    14             html.Replace("&nbsp;", " ");
    15             html.Replace("&lt;", "<");
    16             html.Replace("&gt;", ">");
    17             html.Replace("&quot;", "\"");
    18 
    19             if (identiftyURL)
    20             {
    21                 Regex linkRegex = new Regex("(http://|www)[\\S]{5,}(?=&nbsp;|\r\n)");
    22 
    23                 MatchCollection regMathes = linkRegex.Matches(html.ToString());
    24 
    25                 int add = 0;
    26 
    27                 foreach (Match match in regMathes)
    28                 {
    29                     string head = string.Format("<a href=\"{0}\">", match.Value[0] == 'h' ? match.Value : "http://" + match.Value);
    30 
    31                     html.Insert(match.Index + add, head);
    32                     add += head.Length;
    33 
    34                     html.Insert(match.Index + match.Length + add, "</a>");
    35                     add += 3;
    36                 }
    37             }
    38 
    39             html.Replace("\r\n", "<br />");
    40 
    41             return html.ToString();
    42         }
    43 
    44 
    45 
    46         #region 过滤掉 html代码
    47         private static string StripHTML(string strHtml)
    48         {
    49             #region
    50             string[] aryReg ={ 
    51             @"<script[^>]*?>.*?</script>",  
    52             @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", 
    53             @"([\r\n])[\s]+", 
    54             @"&(quot|#34);", 
    55             @"&(amp|#38);", 
    56             @"&(lt|#60);", 
    57             @"&(gt|#62);", 
    58             @"&(nbsp|#160);", 
    59             @"&(iexcl|#161);", 
    60             @"&(cent|#162);", 
    61             @"&(pound|#163);", 
    62             @"&(copy|#169);", 
    63             @"&#(\d+);", 
    64             @"-->", 
    65             @"<!--.*\n" 
    66             };
    67 
    68             string[] aryRep = { 
    69             "", 
    70             "", 
    71             "", 
    72             "\"", 
    73             "&", 
    74             "<", 
    75             ">", 
    76             " ", 
    77             "\xa1",//chr(161), 
    78             "\xa2",//chr(162), 
    79             "\xa3",//chr(163), 
    80             "\xa9",//chr(169), 
    81             "", 
    82             "\r\n", 
    83             "" 
    84             };
    85             #endregion
    86             string newReg = aryReg[0];
    87             string strOutput = strHtml;
    88             for (int i = 0; i < aryReg.Length; i++)
    89             {
    90                 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    91                 strOutput = regex.Replace(strOutput, aryRep[i]);
    92             }
    93             strOutput.Replace("<", "");
    94             strOutput.Replace(">", "");
    95             strOutput.Replace("\r\n", "");
    96             return strOutput;
    97         }
    98         #endregion
  • 相关阅读:
    Activity详解
    Log和LogCat的使用
    Android Studio项目目录结构
    Android系统架构
    [SCOI2016]美味
    [SCOI2016]背单词
    [SCOI2016]幸运数字
    [BZOJ4170]极光
    [JSOI2016]扭动的回文串
    [SCOI2016]萌萌哒
  • 原文地址:https://www.cnblogs.com/ganler1988/p/3077685.html
Copyright © 2011-2022 走看看